How can I sort a cell array in size, and then, in absolute value
1 view (last 30 days)
Show older comments
Hi everyone.
Ive got this array, its a {1,3} cell array, the first array in the cell is an [Inf;Inf] vector, the second array is [1;1], and the third array is a 2x6 matrix
The thing is, first i want to sort this array by size, so i have this code
[~,I] = sort(cellfun('size',Basins,2),'ascend');
Basins = Basins(I)
and it returns the cell sorted by columns size, as desired.
But then, for the case where ive got columns with the same size ([Inf;Inf] and [1;1]) i want to sort just those 2 columns by the sum of absolute values
so, if my cell array is {[2x6],[1,1],[Inf;inf]} after the first sort it would be {[Inf;inf],[1,1],[2x6]} but then, i would like to sort it so it gives me {[1,1],[Inf;inf],[2x6]}
Thanks in advance :)
0 Comments
Accepted Answer
Maadhav Akula
on 15 Mar 2020
Hi,
I think there was some typo in your question, you have said columns with same size and mentioned correctly in one case([Inf;Inf] and [1;1]), but you have mentioned {[2x6],[1,1],[Inf;inf]} incorrectly a row vector. Irrespective of that the sort function will return the values in the same order as the values were passed in if the size is the same. So I think you have to probably write some code to rearrange if there are elements of the same size and probably the following might be useful:
Basins = {ones(2,6),[Inf;Inf],[1;1],[Inf,Inf],[1,1]};
[sz,I] = sort(cellfun('size',Basins,2),'ascend');
Basins = Basins(I);% Basins = {{[Inf;Inf],[1;1],[Inf,Inf],[1,1],2x6 double}}
d = diff(sz);
l = (find(d == 0));%Finding the arrays of similar size
%Sorting them by mean if they have the same size
for i=1:1:length(l)
k = l(i);
C = Basins(k:k+1);
[~,J] = sort(cellfun(@mean,C),'ascend');% You can write your custom function here instead of mean
C = C(J);
Basins(k:k+1) = C;
end
%Basins = {{[1;1],[Inf;Inf],[1,1],[Inf,Inf],2x6 double}}
Hope this Helps!
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!