How can I sort a cell array in size, and then, in absolute value

1 回表示 (過去 30 日間)
David Valle
David Valle 2020 年 3 月 2 日
コメント済み: David Valle 2020 年 3 月 15 日
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 :)

採用された回答

Maadhav Akula
Maadhav Akula 2020 年 3 月 15 日
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!
  1 件のコメント
David Valle
David Valle 2020 年 3 月 15 日
It works perfectly
I want you to know that you just made my day :)
Thank you very much :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by