row circular shift in matrix
조회 수: 9 (최근 30일)
이전 댓글 표시
How can I shift all the elements of a particular row in matrix in left circular shift or right circular shift.
댓글 수: 0
채택된 답변
Tommy
2020년 4월 20일
편집: Tommy
2020년 4월 23일
M(row,:) = [M(row,end) M(row,1:end-1)]; % shift to the right
M(row,:) = [M(row,2:end) M(row,1)]; % shift to the left
(edit) To shift by any amount:
M = randi(10,5)
shift = 8; row = 2;
[n,m] = size(M);
M(row,:) = [M(row,(end-mod(shift,m)+1):end) M(row,1:(end-mod(shift,m)))] % shift to the right
M(row,:) = [M(row,(mod(shift,m)+1):end) M(row,1:mod(shift,m))] % shift to the left
(edit) Fixed mistake
댓글 수: 3
Tommy
2020년 4월 23일
Yes sorry I goofed! I used n where I should've used m. Let me know if that fixes it for you.
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!