MATLAB: How to use for loop

for loop

Hi every one briefly, I have i=3:5 and j=1:3 and My equation is how to let i=3 for calculating the first raw and let j changes from 1:3 and for second raw i =4 and let j changes from 1:3 and third raw i =4 and let j changes from 1:3 and third
% Calculate the first row.
i=3
for j=1:3
A(i,j)=(A(i-1,1)*A(i-2,j+1)-A(i-1,j+1)*A(i-2,1))/(A(i-1,1))
end
% Calculate the second row
i=4
for j=1:3
A(i,j)=(A(i-1,1)*A(i-2,j+1)-A(i-1,j+1)*A(i-2,1))/(A(i-1,1))
end
% Calculate the third row
i=5
for j=1:3
A(i,j)=(A(i-1,1)*A(i-2,j+1)-A(i-1,j+1)*A(i-2,1))/(A(i-1,1))
end

Best Answer

Try this:
clc; % Clear command window.
A = magic(5); % Create some sample data.
% Calculate the first row.
for row = 3 : 5
for column = 1 : 3
A(row, column)= ...
(A(row-1, 1) * A(row-2, column+1) - ...
A(row-1, column+1) * A(row-2, 1)) / ...
A(row-1, 1);
end
end
A % Print to command window.