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

I have a matrix 12528x246, 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.

댓글 수: 1

KL
KL 2017년 12월 15일
편집: KL 2017년 12월 15일
For e.g, 1:6(perform circular shift for 1 position) then repeat the steps for 7:12
repeat how? by increasing the shift value by 2,3 and so on?

댓글을 달려면 로그인하십시오.

 채택된 답변

KL
KL 2017년 12월 15일
편집: KL 2017년 12월 15일
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

추가 답변 (4개)

Guillaume
Guillaume 2017년 12월 15일
If I understood correctly, all you have to do is reshape your original array into rows or columns of 6 elements, perform the circular shift, and reshape back. Because you want to operate on columns and matlab is row based, you'll have to transpose back and forth, so:
shift = 1; %or -1 maybe, you haven't specified the direction
shiftedmatrix = reshape(circshift(reshape(yourmatrix.', 6, []), shift, 1).', size(yourmatrix))
Andrei Bobrov
Andrei Bobrov 2017년 12월 15일
편집: Andrei Bobrov 2017년 12월 15일
A - your array
k = 6;
[m,n] = size(A);
out = A1(sub2ind([m,n],repelem(hankel([(2:m)';1],1:n/k),1,k),repelem(1:n,m,1)));
Clever indexing will do the trick:
A = repmat((1:18),10,1); % dummy data with 18 columns
S = 6
ix = 0:size(A,2)-1
ix0 = S * floor(ix/S)
ix1 = rem(ix,S)
ix1shift = rem(ix1+S-1,S)
ix2 = ix0 + ix1shift + 1
B = A(:,ix2)
% all this can be done in one step, of course, which makes it an "easy-to-read" one-liner :D
B2 = A(:,S * floor((0:size(A,2)-1)/S) + rem(rem(0:size(A,2)-1,S)+S-1,S)+1)
arun
arun 2017년 12월 15일

0 개 추천

Thank you all for your ideas and contributions, but it seems cell2mat concept is easier than other ideas

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2017년 12월 15일

편집:

KL
2017년 12월 15일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by