フィルターのクリア

How can I sort this cell Array?

6 ビュー (過去 30 日間)
Afsane Afsane
Afsane Afsane 2015 年 5 月 4 日
コメント済み: Michael Haderlein 2015 年 5 月 5 日
Hello
I have a cell arraye like this:
ca{1,1}={'x' ;'y'; 'z'};
ca{1,2}=[1 1 9 3;...
5 1 1 1;...
5 7 5 9];
ca{1,3}={13; 14 ; 10};
How can I sort this cell Array based on last column ( ca{1,3})?
  1 件のコメント
Afsane Afsane
Afsane Afsane 2015 年 5 月 4 日
I shouldn't convert the cell array to array. Is there any function to sort the array based on last column directly? or at least sort the first column (string column) based on last column.

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2015 年 5 月 4 日
[~,ii] = sort(cat(1,ca{3}{:}));
out = cellfun(@(x)x(ii,:),ca,'un',0);
  3 件のコメント
Afsane Afsane
Afsane Afsane 2015 年 5 月 5 日
Thanks a lot! But I didn't understand what this function return and what are its inputs:
out = cellfun(@(x)x(ii,:),ca,'un',0);
Michael Haderlein
Michael Haderlein 2015 年 5 月 5 日
It applies a function on each element of a cell. The cell is ca and the function is @(x)x(ii,:). x is the cell element here. So in the end, it just applies the sorting array ii on each element of the cell. The 'un',0 is an additional parameter which is required whenever the output is a cell and not a numerical array. Just play with cellfun, you'll understand it easily.

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

その他の回答 (2 件)

dpb
dpb 2015 年 5 月 4 日
>> [~,ix]=sort(cell2mat(ca{1,3}));
>> ca{1,2}=ca{1,2}(ix,:);
>> ca{1,2}
ans =
5 1 1 1
5 7 5 9
1 1 9 3
>>
Having the last cell as an array of three elements instead of a 3-vector makes it tougher--otherwise could do away w/ the cell2mat and just use the curlies to address the cell for sorting.

Afsane Afsane
Afsane Afsane 2015 年 5 月 4 日
編集済み: Afsane Afsane 2015 年 5 月 4 日
I shouldn't convert the cell array to array. Is there any function to sort the array based on last column directly? or at least sort the first column (string column) based on sorted last column.
  8 件のコメント
Mohammad Abouali
Mohammad Abouali 2015 年 5 月 5 日
編集済み: Mohammad Abouali 2015 年 5 月 5 日
cat(1,ca{3}{:}) in Andrei Bobrov's answer is still converting a cell array to regular array then sorts it. That's not any different than dpb's first answer using cell2mat.
The problem is that she wants to do it without any conversion to other data format.
Afsane Afsane
Afsane Afsane 2015 年 5 月 5 日
I think I have a problem in initialize the cell array if I introduce the cell array like for example :
m={'a' 10; 'b' 12;'c' 9}
then I can use sortrows :
m1=sortrows(m,2)
but I have to change other codes :(

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

カテゴリ

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