sorting sturctured variable

1 回表示 (過去 30 日間)
ramakrishna bathini
ramakrishna bathini 2011 年 10 月 4 日
hi guys, I need a way to sort the structured variable based on the size of the elements present in those respective structures.
CC=bwconncomp(imstack,26);
stats = regionprops(CC,'pixellist');
statss(8846,1)=0;
for i=1:8846
statss(i,1) = size(stats(i,1).PixelList,1);
end
statss=sort(statss,1);
statss gives the number on which the respective stats should be sorted...What matlab commands can i use to sort structure?

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 10 月 4 日
I don't think you can sort structure array directly. However, you can get the size of the field of each structure, then sort to get the index and then re-arrange the structure array using the index, see example:
s=struct('f',{rand(1),rand(3),rand(2)});
b=cellfun('size',{s.f},1);
[c,index]=sort(b);
NewS=s(index);
NewS.f
NewS =
1x3 struct array with fields:
f
ans =
0.9049
ans =
0.2217 0.2967
0.1174 0.3188
ans =
0.9797 0.2581 0.2622
0.4389 0.4087 0.6028
0.1111 0.5949 0.7112

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 10 月 4 日
Replace
statss=sort(statss,1);
with
[statss, statidx] = sort(statss,1);
sortedstats = stats(statidx);

カテゴリ

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