How to rearrange columns in a MATRIX?

29 ビュー (過去 30 日間)
Leeba Ann Chacko
Leeba Ann Chacko 2022 年 9 月 4 日
移動済み: Walter Roberson 2022 年 9 月 5 日
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
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)]
y = 4×2
17.0600 29.6780 19.0280 42.3270 23.0660 29.4160 22.7940 43.0240
Leeba Ann Chacko
Leeba Ann Chacko 2022 年 9 月 4 日
I see. The problem is that in this example, I have only 4 columns but I have an array with several martrices that have 2 rows but different column numbers. However, for all of them, I want to rearrange the matrix the way I did in the example above.
ex. 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]
i.e
1 2 4 4 5 6
3 4 6 6 7 8
3 4 5 6 6 7
4 5 6 8 7 8
rearranged to
1 2
3 4
3 4
4 5
4 4
6 6
5 6
6 8
5 6
7 8
6 7
7 8
I want to write a for loop to automate this for all the matrices with different column numbers. How do I write the code without specifically indicating the column numbers like you did in your solution?

サインインしてコメントする。

採用された回答

Dyuman Joshi
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)]
y = 12×2
1 2 3 4 3 4 4 5 4 4 6 6 5 6 6 8 5 6 7 8
  1 件のコメント
Leeba Ann Chacko
Leeba Ann Chacko 2022 年 9 月 4 日
移動済み: Walter Roberson 2022 年 9 月 5 日
This works perfectly, thank you!

サインインしてコメントする。

その他の回答 (2 件)

Stephen23
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]
x = 4×6
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)
y = 12×2
1 2 3 4 3 4 4 5 4 4 6 6 5 6 6 8 5 6 7 8
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 9 月 4 日
Yes, permute() is the way to go here.

サインインしてコメントする。


James Tursa
James Tursa 2022 年 9 月 4 日
One way:
[X(:,1:2);X(:,3:4)]
  2 件のコメント
Leeba Ann Chacko
Leeba Ann Chacko 2022 年 9 月 4 日
Is there a way to do this without specifically indicating the column numbers like you did in your solution because I have an array with mulitple matrices containing 2 rows but different column numbers each. I cannot specify the same column numbers for all of them since I want to automate it using a for loop.
James Tursa
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 ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by