Reputation: 87
I am looking to apply an IF statement to a matrix like the following:
A =
2 5 2 4 8
2 5 0 0 0
2 4 0 0 0
6 8 0 0 0
9 5 0 0 0
4 7 0 0 0
8 1 0 0 0
What I have so far is this:
if A(1,2)>A(1,4),
A(2,3)=A(1,4);
A(2,4)=(A(1,5))+1;
end
if A(1,2)<A(1,4),
A(2,4)=(A(1,4))-1;
A(2,4)=(A(1,4))-(A(2,3));
end
In the aforementioned code I am only comparing the 2nd and 4th column of the first row. Then the first row constructs the second row.
I am looking to then compare the second row and use that to construct the third row ... and so on ... throughout all 7 rows.
Could I add a 1:n to modify this?
Any suggestions?
Upvotes: 2
Views: 388
Reputation: 11810
You can write the following loop
for i=1:size(A, 1)-1
if A(i,2)>A(i,4),
A(i+1,3)=A(i,4);
A(i+1,4)=A(i,5)+1;
end
if A(i,2)<A(i,4),
A(i+1,4)=A(i,4)-1;
% wrong index here? You assign twice to the same A entry.
% the above line has no effect...
A(i+1,4)=A(i,4)-A(i+1,3);
end
end
Upvotes: 1