MATLAB: I have 2 for commands, and only one works?:O

for loopMATLABmatrix

I have 2 matrix, for example A(nx3) and B(mx3), and in the same code I have to change with same for loop something in them. But only changes one, other stays the same, what can be problem?
Y and Z can be any numbers that can be divided by size of matrix(if matrix are 20×3, Y or Z can be 2,4,5,10) code:
for k=0:100;
A(k*(2*Y)+(Y+1):k*(2*Y)+(2*Y),1:3)=A(k*(2*Y)+(2*Y):-1:k*(2*Y)+(Y+1),1:3);
end;
for j=0:100;
B(j*(2*Z)+(Z+1):j*(2*Z)+(2*Z),1:3)=B(j*(2*Z)+(2*Z):-1:j*(2*Z)+(Z+1),1:3);
end;

Best Answer

I assume that k = 0:100 is a mistake. For your code to work k can't go higher than size(A, 1)/2*Y - 1.
It would appear that your code flip up down half of the rows of your matrices. That can be achieved simply without a loop:
A = reshape(A, 2*Y, [], size(A, 2));
A(Y+1:2*Y, :, :) = A(2*Y:-1:Y+1, :, :);
A = reshape(A, [], size(A, 3))