sorting numerically in a cell array

21 ビュー (過去 30 日間)
Will Swaston
Will Swaston 2018 年 5 月 9 日
回答済み: Image Analyst 2018 年 5 月 9 日

Hi everyone, I have a cell array that looks like this...

{'TS-G8-box7-017_0000_-0.…'} {'box7'} {'017'} {'-0.0' } {'box7_tomo_017'}

{'TS-G8-box7-017_0010_-10…'} {'box7'} {'017'} {'-10.0'} {'box7_tomo_017'}

{'TS-G8-box7-017_0016_-12…'} {'box7'} {'017'} {'-12.0'} {'box7_tomo_017'}

{'TS-G8-box7-017_0017_-14…'} {'box7'} {'017'} {'-14.0'} {'box7_tomo_017'}

{'TS-G8-box7-017_0018_-16…'} {'box7'} {'017'} {'-16.0'} {'box7_tomo_017'}

{'TS-G8-box7-017_0019_-18…'} {'box7'} {'017'} {'-18.0'} {'box7_tomo_017'}

{'TS-G8-box7-017_0006_-2.…'} {'box7'} {'017'} {'-2.0' } {'box7_tomo_017'}

{'TS-G8-box7-017_0020_-20…'} {'box7'} {'017'} {'-20.0'} {'box7_tomo_017'}

{'TS-G8-box7-017_0026_-22…'} {'box7'} {'017'} {'-22.0'} {'box7_tomo_017'}

{'TS-G8-box7-017_0027_-24…'} {'box7'} {'017'} {'-24.0'} {'box7_tomo_017'}

I'd like to sort it by column 4 numerically, I tried using sortrows() however this sorts it alphabetically, how would I go about sorting it numerically? Thanks, Will

回答 (1 件)

Image Analyst
Image Analyst 2018 年 5 月 9 日
Try this:
% Define cell array
ca = {...
'TS-G8-box7-017_0000_-0.…', 'box7', '017', '-0.0' , 'box7_tomo_017';
'TS-G8-box7-017_0010_-10…', 'box7', '017', '-10.0', 'box7_tomo_017';
'TS-G8-box7-017_0016_-12…', 'box7', '017', '-12.0', 'box7_tomo_017';
'TS-G8-box7-017_0017_-14…', 'box7', '017', '-14.0', 'box7_tomo_017';
'TS-G8-box7-017_0018_-16…', 'box7', '017', '-16.0', 'box7_tomo_017';
'TS-G8-box7-017_0019_-18…', 'box7', '017', '-18.0', 'box7_tomo_017';
'TS-G8-box7-017_0006_-2.…', 'box7', '017', '-2.0' , 'box7_tomo_017';
'TS-G8-box7-017_0020_-20…', 'box7', '017', '-20.0', 'box7_tomo_017';
'TS-G8-box7-017_0026_-22…', 'box7', '017', '-22.0', 'box7_tomo_017';
'TS-G8-box7-017_0027_-24…', 'box7', '017', '-24.0', 'box7_tomo_017'}
% Extract column 4 and convert to doubles.
column4 = str2double(ca(:,4))
% Sort numerically.
[sortedValues, sortOrder] = sort(column4);
% Apply the sort order to the original cell array, creating a new cell array, ca2.
ca2 = ca(sortOrder, :)

カテゴリ

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