How can I sort a cell array in size, and then, in absolute value
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 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!