MATLAB: Write a for loop that goes through each row of Matrix

forloopindexindexingloopMATLABmatrix

Hi
I'm kinda stuck here, how to write a for-loop that goes through each row of matrix Z and does the following:
  • If the second column value is 1, display the first column value;
  • if the second column value is 2, display the negative of the first column value;
  • if it is 3 then times the first column by 2
Here's what I currently have:
for i = 1:size(Z,1)
if Z(i,2)==1
value1 = Z(i,1)
disp(value1)
else Z(i,2)==2
value2 = Z(i,1)<0
disp(value2)
end
if Z(i,2)==3
value3 = Z(i,1)*5
disp(value3)
end
end
The Z can be any matrix, e.g.,
Z = [27 3;
55 3;
53 2;
24 3;
49 1;
63 1];

Best Answer

for i = 1:size(Z,1)
and refer to Z(i,1) and Z(i,2)