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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!