If A is a matrix, A(:) produces the columns in a vector. Is there a parallel function for rows?

228 ビュー (過去 30 日間)
Rajmohan
Rajmohan 2016 年 8 月 26 日
編集済み: James Tursa 2016 年 8 月 26 日
A=[1,2,3;4,5,6;7,8,9]. Then A(:) returns [1;2;3;4;5;6;7;8;9]. Is there an operator that will produce [1,2,3,4,5,6,7,8,9]?

回答 (1 件)

James Tursa
James Tursa 2016 年 8 月 26 日
編集済み: James Tursa 2016 年 8 月 26 日
First, I assume your example result is a typo, since A(:) will return the elements in column order, not row order. E.g.,
>> A=[1,2,3;4,5,6;7,8,9]
A =
1 2 3
4 5 6
7 8 9
>> A(:)
ans =
1
4
7
2
5
8
3
6
9
To get them out in row order, transpose A first. E.g.
>> A=[1,2,3;4,5,6;7,8,9]
A =
1 2 3
4 5 6
7 8 9
>> reshape(A',[],1)
ans =
1
2
3
4
5
6
7
8
9
  2 件のコメント
Rajmohan
Rajmohan 2016 年 8 月 26 日
Both return [1,4,7,2,5,8,3,6,9] instead of [1,2,3,4,5,6,7,8,9]
James Tursa
James Tursa 2016 年 8 月 26 日
編集済み: James Tursa 2016 年 8 月 26 日
Ummm ... that was a direct copy from my MATLAB command window, and the order is clearly 1,2,3,4,5,6,7,8,9 as shown. If you want the result as a row instead of a column, then simply:
reshape(A',1,[])

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by