MATLAB: How to circular shift a matrix for every 1 to 6 elements until its end

matrixmatrix arraymatrix manipulation

I have a matrix 12528×246, I would like to the circular shift of the 2nd dimension(246). The shift should be done for every consecutive 6 elements from 1 position till the end of the matrix starting from 1 to 246. For e.g, 1:6(perform circular shift for 1 position) then repeat the steps for 7:12,……up to 241:246. Is it possible to do such an operation using circular shift? I tried my best but couldn't find the correct logic.

Best Answer

I'm only guessing. If you want to do circshifts for blocks of columns, use mat2cell first, do the shifting and convert it back using cell2mat. Small example,
A = repmat((1:18),10,1); %dummy data with 18 columns
sz = size(A);
splits = 6;
B = mat2cell(A,sz(1),repmat(splits,1,sz(2)/splits));
B_shifted = cell2mat(cellfun(@(x) circshift(x,1,2),B,'uni',0));
A =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
B_shifted =
6 1 2 3 4 5 12 7 8 9 10 11 18 13 14 15 16 17
6 1 2 3 4 5 12 7 8 9 10 11 18 13 14 15 16 17
6 1 2 3 4 5 12 7 8 9 10 11 18 13 14 15 16 17
6 1 2 3 4 5 12 7 8 9 10 11 18 13 14 15 16 17
6 1 2 3 4 5 12 7 8 9 10 11 18 13 14 15 16 17
6 1 2 3 4 5 12 7 8 9 10 11 18 13 14 15 16 17
6 1 2 3 4 5 12 7 8 9 10 11 18 13 14 15 16 17
6 1 2 3 4 5 12 7 8 9 10 11 18 13 14 15 16 17
6 1 2 3 4 5 12 7 8 9 10 11 18 13 14 15 16 17
6 1 2 3 4 5 12 7 8 9 10 11 18 13 14 15 16 17