Sorting the rows of a 2-column matrix based on entries from one column.

1 回表示 (過去 30 日間)
Kyle Reagan
Kyle Reagan 2017 年 6 月 16 日
コメント済み: Kyle Reagan 2017 年 6 月 16 日
I have a matrix with two columns. The first column is numbers and the second is a string of words. I want to sort the entire matrix in ascending order based on the first column entries and without losing each number's second column pair. A simplified example is as follows:
4 Apple
3 Orange
6 Pear
1 Peach
to
1 Peach
3 Orange
4 Apple
6 Pear
  2 件のコメント
the cyclist
the cyclist 2017 年 6 月 16 日
A MATLAB matrix cannot hold text. Is this a cell array?
Kyle Reagan
Kyle Reagan 2017 年 6 月 16 日
Whoops, yes it is a cell array. Thanks!

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

採用された回答

the cyclist
the cyclist 2017 年 6 月 16 日
編集済み: the cyclist 2017 年 6 月 16 日
C = {4, 'Apple';
3, 'Orange';
6, 'Pear';
1, 'Peach'};
sortrows(C)
ans =
4×2 cell array
[1] 'Peach'
[3] 'Orange'
[4] 'Apple'
[6] 'Pear'
See the documentation for sortrows for details.

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by