MATLAB: Column circular permutaion on a matrix

column circular permutationMATLAB

How can I move all the elements of a particular column circulary upwards or circularly downwards by some shift s. I have a matrix A = [ 1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16] and shift columns 1, 2, 3 and 4 by shifts 2, 1 , 3 and 2 respectively then I get the matrix A as A = [9 6 15 12; 13 10 3 16; 1 14 7 4; 5 2 11 8].

Best Answer

Try this
A = [ 1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
B = [9 6 15 12; 13 10 3 16; 1 14 7 4; 5 2 11 8];
shift = [2, 1, 3, 2];
C = A; % make a copy
for i=1:numel(shift)
C(:,i) = circshift(C(:,i), -shift(i));
end
Result
>> isequal(B, C)
ans =
logical
1