MATLAB: Matrix from a for FOR loop with IF conditioning

matlab r2013b

I have the following problem:
A = [1 2 3; 4 5 6; 7 8 9]
for i = 1:n
if rem(i,2)== 0
x = fliplr(D(i,:))
else x = D(i,:)
end
B(i) = x
end
I'm trying to get the B matrix that should look like
B = [1 2 3; 6 5 4; 7 8 9]
but I get the following error message instead
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in back_and_forth (line 16)
B(i) = x
Could anyone help me with this?

Best Answer

A = [1 2 3; 4 5 6; 7 8 9]
A(2:2:end,:)=fliplr(A(2:2:end,:))
%Or if you want to do it with a for loop
A = [1 2 3; 4 5 6; 7 8 9]
n=size(A,1)
B=A;
for i = 1:n
if rem(i,2)== 0
B(i,:) = fliplr(A(i,:))
end
end