How to rearrange columns in a MATRIX?
조회 수: 28 (최근 30일)
이전 댓글 표시
I have a 2x4 matrix 'X' which looks like the follwing
X = 17.0600 29.6780 23.0660 29.4160
19.0280 42.3270 22.7940 43.0240
I would like the last 2 columns to come below the 1st 2 column so that it will look like this
X= 17.0600 29.6780
19.0280 42.3270
23.0660 29.4160
22.7940 43.0240
How do I go about this using he reshape function?
댓글 수: 2
Dyuman Joshi
2022년 9월 4일
편집: Dyuman Joshi
2022년 9월 4일
I don't think that is possible using reshape (alone), as you are changing the order of elements. You can use vertical concatention to get the desired result -
x=[17.0600 29.6780 23.0660 29.4160;19.0280 42.3270 22.7940 43.0240];
y=[x(:,1:2);x(:,3:4)]
채택된 답변
Dyuman Joshi
2022년 9월 4일
이동: Walter Roberson
2022년 9월 5일
x=[1 2 4 4 5 6; 3 4 6 6 7 8; 3 4 5 6 6 7;4 5 6 8 7 8];
y=[reshape(x(:,1:2:end),[],1) reshape(x(:,2:2:end),[],1)]
추가 답변 (2개)
Stephen23
2022년 9월 4일
x = [1,2,4,4,5,6; 3,4,6,6,7,8; 3,4,5,6,6,7; 4,5,6,8,7,8]
y = reshape(permute(reshape(x,size(x,1),2,[]),[1,3,2]),[],2)
James Tursa
2022년 9월 4일
One way:
[X(:,1:2);X(:,3:4)]
댓글 수: 2
James Tursa
2022년 9월 4일
편집: James Tursa
2022년 9월 4일
To receive quality answers, it is best to describe the complete problem when first posting your question. E.g., Do you want the result to always be 2 columns? Or 1/2 the original columns? Etc.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!