Shift rows by different amounts
조회 수: 19 (최근 30일)
이전 댓글 표시
I have a N^2 x N^2 matrix where N = 4, and I wish to shift rows 1, 5, 9, and 13 by 0. Rows 2, 6, 10, 14 by 4. Rows 3, 7, 11, 15 by 8. Finally rows 4, 8, 12, 16 by 12. Ideally it would be a general code, as I plan to apply it to more than just this example. I have tried many things but to no avail. Any help would be appreciated. Thanks
댓글 수: 0
채택된 답변
Cedric
2015년 7월 31일
편집: Cedric
2015년 7월 31일
N = 4 ;
A = repmat( 1:N^2, N^2, 1 ) ; % Dummy example.
for k = 2 : N
A(k:N:N^2,:) = circshift( A(k:N:N^2,:), (k-1) * N, 2 ) ;
end
With that, the the original A is:
A =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
and the final:
A =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12
9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8
5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12
9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8
5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12
9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8
5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12
9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8
5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4
I am not sure that the solutions that don't involve a FOR loop are more efficient ultimately, because the FOR loop has only N-1 iterations and no realloc or conversion to/from cell arrays. You'd have to profile every approach to be sure.
댓글 수: 2
추가 답변 (1개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!