필터 지우기
필터 지우기

Move a columns to the right

조회 수: 8 (최근 30일)
Nour BS
Nour BS 2016년 1월 10일
댓글: Star Strider 2016년 1월 10일
Hello everyone, How can I move/shift a set of columns in an array to the right? For example I have
B= [1 3 4 5 2 4; 2 8 6 7 9 8]
and I want to get this result:
S= [1 3 2 4 5 4; 2 8 9 6 7 8].
How can I do this? thank you in advance.

채택된 답변

Star Strider
Star Strider 2016년 1월 10일
The only way I can think of to do it is to use the circshift function on columns 3:5:
S = [B(:,1:2) circshift(B(:,3:5),[0 1]) B(:,6)];
  댓글 수: 6
Nour BS
Nour BS 2016년 1월 10일
thanks a lot :))))))
Star Strider
Star Strider 2016년 1월 10일
As always, my pleasure!
If you want to rotate your matrices up-down one position instead of left-right one position, the circshift call would be:
S = circshift(B, [-1 0]);
With only two rows, the sign doesn’t have any noticeable effect. With larger matrices, it would.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by