필터 지우기
필터 지우기

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

조회 수: 2 (최근 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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by