how to sort within the cell array

9 ビュー (過去 30 日間)
MakM
MakM 2022 年 12 月 26 日
回答済み: dpb 2022 年 12 月 26 日
I have the following code;
sec=({'a';'a';'b';'b';'c';'c';'a';'b'});
fir=({0;0;0;1;2;3;4;0});
all=[fir,sec];
[~,~,X] = unique(all(:,2));
all_data= accumarray(X,1:size(all,1),[],@(r){all(r,:)})
all_data = 3×1 cell array
{3×2 cell} {3×2 cell} {2×2 cell}
I want to sort all data cell array according to first digit; for example: if we see the second cell array
all_data{2,:}
ans = 3×2 cell array
{[0]} {'b'} {[1]} {'b'} {[0]} {'b'}
First row is, 0 1 0, I want this to be 0 0 1, how can I sort these cell arrays after accumaarray function?

採用された回答

dpb
dpb 2022 年 12 月 26 日
>> [~,ix]=sort(cell2mat(tmp(:,1)));
>> tmp=tmp(ix,:)
tmp =
3×2 cell array
{[ 0]} {'b'}
{[ 0]} {'b'}
{[1.00]} {'b'}
>>
to do the one cell...you'll probably have to wrap this in an explicit loop over the size of the input cell array; I couldn't see a convenient way to dereference in anonymous function even with my local utility function sortIndex that returns the order permutation vector instead of the sorted values.

その他の回答 (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