MATLAB: How to use elements i, j to print a 5×5 matrix

5x5 matrixfor loopfprintfi j

I want to achieve what is in the comment of the code but using my code the for loop does not effect the result. How can I produce the matrix with the provided equation?
% 5x5 matrix, with elements given by A(i,j)= 1/(2^i+ 3^j). Print entire
% matrix, first row and second column. Two ways of displaying A(3,2)
clc
close all
A = magic(i : j)
for i = 1 : 5
for j = 1 : 5
A(i,j) = 1./(2^i+3^j);
end
end
fprintf('A(i, j)\n');
fprintf('%5.1f \t %5.1f \t %5.1f \t %5.1f \t %5.1f\n', [magic(i:j)])
result:
A(i, j)
17.0 23.0 4.0 10.0 11.0
24.0 5.0 6.0 12.0 18.0
1.0 7.0 13.0 19.0 25.0
8.0 14.0 20.0 21.0 2.0
15.0 16.0 22.0 3.0 9.0

Best Answer

>> [I,J] = ndgrid(1:5);
>> A = 1./(2.^I+3.^J);
>> F = [repmat(' %9.7f',1,5),'\n'];
>> fprintf(F,A.') % note the tranpose!
0.2000000 0.0909091 0.0344828 0.0120482 0.0040816
0.1428571 0.0769231 0.0322581 0.0117647 0.0040486
0.0909091 0.0588235 0.0285714 0.0112360 0.0039841
0.0526316 0.0400000 0.0232558 0.0103093 0.0038610
0.0285714 0.0243902 0.0169492 0.0088496 0.0036364
MATLAB stores matrices columnwise, so that tranpose is important!