Merging row elements into single numbers

16 ビュー (過去 30 日間)
Aleem Andrew
Aleem Andrew 2021 年 10 月 31 日
編集済み: Chris 2021 年 10 月 31 日
If you have the 3*3 matrix
C = [3 4 5
6 7 9
1 5 8]
and you want to change it to the 3*1 matrix
C = [345
679
158]
so that you can for example sort it in descending order of the rows, how would you do this? Thank you.

採用された回答

Chris
Chris 2021 年 10 月 31 日
編集済み: Chris 2021 年 10 月 31 日
Edit: Use Star Strider's answer. It's faster.
C = [3 4 5
6 7 9
1 5 8]
C = 3×3
3 4 5 6 7 9 1 5 8
C = double(string(C).join(''))
C = 3×1
345 679 158
  3 件のコメント
Star Strider
Star Strider 2021 年 10 月 31 日
Another approach —
C = [3 4 5
6 7 9
1 5 8]
C = 3×3
3 4 5 6 7 9 1 5 8
C*10.^[2;1;0]
ans = 3×1
345 679 158
Just thought I’d add that for fun!
.
Chris
Chris 2021 年 10 月 31 日
Interesting. This was probably the most obvious method before strings? Easily generalized for an array of single-digit integers:
C*10.^[size(C,2)-1:-1:0]'

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by