How do I turn a nxm matrix into a single array?

31 ビュー (過去 30 日間)
Ahsan Khan
Ahsan Khan 2012 年 10 月 17 日
hi Matlabers,
say I have a matrix
[1 2 3 4;5 6 7 8;9 10 11 12; 13 14 15 16]
how can I turn this matrix so all the rows are combined into one. one after the other.
result
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]?
thanks in advance. cheers SN

採用された回答

Sachin Ganjare
Sachin Ganjare 2012 年 10 月 17 日
編集済み: Sachin Ganjare 2012 年 10 月 17 日
You can use reshape command:
a= [1 2 3 4;5 6 7 8;9 10 11 12; 13 14 15 16]
b = reshape(a,1,16)
or simply
b = a(:)'
Hope it helps!!!
  3 件のコメント
Ahsan Khan
Ahsan Khan 2012 年 10 月 17 日
thanks so much...just what i needed
Ahsan Khan
Ahsan Khan 2012 年 10 月 17 日
thanks for all the help. but one last question, now say if I wanted to plot this in increments of 4. so it would plot columns 1:4 then i press enter and it plots 5:8... and so on. so it would look something like:
for i = 1:length(b)
plot(i,b(1:4)) % <<I KNOW THIS ISNT RIGHT...HOW DO I modify this..pls?
pause
end
am I even on the right track? thanks in advance

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

その他の回答 (1 件)

manoj saini
manoj saini 2012 年 10 月 17 日
to exact answer >>b=reshape(a',1,16) because matlab always prefer coloum element so pass a'(a transpose) for your exact output
  1 件のコメント
Ahsan Khan
Ahsan Khan 2012 年 10 月 17 日
thank you!!!:)

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

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by