行列の形状変更をうまく表現できません

例えば次のような行列Aがあるとします.
A =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
このとき,形状変更をして以下のような行列Bとしたいです.
B =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
”配列の形状変更”を参考にreshapeを試しましたが以下のようになりました.
>> B = reshape (A,[1,3*5])
B =
1 6 11 2 7 12 3 8 13 4 9 14 5 10 15

 Akzeptierte Antwort

Kojiro Saito
Kojiro Saito am 29 Jun. 2022

1 Stimme

reshapeは列優先で計算されるので、transposeまたは「'」を使って行列Aを転置してからreshapeすれば実現できます。
参考:transpose, .'
A = [1:5;6:10;11:15]
A = 3×5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
B = reshape(A', 1,15)
B = 1×15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

2 Kommentare

知旺 高田
知旺 高田 am 29 Jun. 2022
回答ありがとうございます。実行してみます。
Hernia Baby
Hernia Baby am 30 Jun. 2022
ちょっと冗長になりますが以下のようなやり方もあります。
A = [1:5;6:10;11:15];
A = A'
A = 5×3
1 6 11 2 7 12 3 8 13 4 9 14 5 10 15
B = A(:)'
B = 1×15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
またreshapeを用いる場合は任意のデータサイズに対応するため、
15の部分を [] に置き換えることをオススメします。
B = reshape(A,1,[])
B = 1×15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 行列および配列 finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!