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일

0 개 추천

[~,ii] = sort(cat(1,ca{3}{:}));
out = cellfun(@(x)x(ii,:),ca,'un',0);

댓글 수: 3

Michael Haderlein
Michael Haderlein 2015년 5월 5일
편집: Michael Haderlein 2015년 5월 5일
To address the constraints of the teacher, replace the first line with
[~,ii]=sortrows(ca{1,3});
and use the second line as Andrei stated.
>> out{1}
ans =
'z'
'x'
'y'
>> out{2}
ans =
5 7 5 9
1 1 9 3
5 1 1 1
>> out{3}
ans =
[10]
[13]
[14]
That's also what Mohammad Abouali suggested.
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일

0 개 추천

>> [~,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일

0 개 추천

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

Well, you shoudna' saved it as separate elements then...as is,
>> ca{1,3}
ans =
[13]
[14]
[10]
>>
The problem as is that although you can write
>> [s,idx]=cellfun(@sort,ca{1,3})
s =
13
14
10
idx =
1
1
1
>>
where you can return the sorted values, since each is a single value instead of being the one-vector the index for each is "1" and so you have no order information available. Only placing the cell as a vector to begin with (and why you wouldn't is hard to see as you have a double array as the second element, why not a vector as the third?) or creating the vector one way or another, either as a temporary as my solution or explicitly as Andrei did will lead to joy...
Mohammad Abouali
Mohammad Abouali 2015년 5월 4일
Why you shouldn't convert Cell Array to regular array?
The data structure that you have chosen to store this data is really not suitable.
Afsane Afsane
Afsane Afsane 2015년 5월 5일
Our Master have solved the problem with regular array ( of course I separated the code that I had problem and it is not the whole code) and He wanted us to solve it with Cell Array and he said that we could work easily with Cell array in this code ( for example use sortrows).
As suggested, make
ca{1,3}={13; 14 ; 10}
instead of
ca{1,3}={13; 14 ; 10};
as you have. THEN you can use sort and friends on the content of that cell by dereferencing with the curlies.
I don't see that sortrows does you much (as in any) good unless you also include the data column by which you wish the ordering to be determined in the array of values; the optional COL input is relative to the columns in the sorted array and you have two arrays, not one here (that they're held in a single higher level cell array is immaterial for thie purpose).
Mohammad Abouali
Mohammad Abouali 2015년 5월 5일
편집: Mohammad Abouali 2015년 5월 5일
I think dpb meant
ca{1,3}=[13; 14 ; 10]
instead of
ca{1,3}={13; 14 ; 10}
Michael Haderlein
Michael Haderlein 2015년 5월 5일
Have you checked Andrei Bobrov's answer? He's addressing the cell issue.
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 :(

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by