How to sort a array of matrix based on the maximum value

10 views (last 30 days)
Hi ,
I have an array D here cell array is have only one row but the conent inside the array have more than 1 rows
D{1,1}=[0 0 1 2 3 ; 0 0 1 0];
D{1,2}=[0 0 4 0 0; 0 0 2 ];
D{1,3}=[0 5 6 0; 0 0 4];
D{1,4}=[4 0; 6 0 0 ]; Is it possible to sort the rows one by one sepeartely I need to keep D{1,1} in the first place but rest have to sort in desending order of their Max value.
The result should be like this :
D{1,1}=[0 0 1 2 3 ; 0 0 1 0];
D{1,2}=[0 5 6 0; 6 0 0 ];
D{1,3}=[0 0 4 0 0; 0 0 4 ];
D{1,4}=[4 0; 0 0 2 ]; I dont know how to use the sort function in this situation. Please suggest the How to implement.
Thanks in advance.
  1 Comment
Chandrashekar D S
Chandrashekar D S on 14 Aug 2020
I have alter the below code from Brouno and it was really helpful.
for j=1:2 %% here 2 rows
for i =1:4
dmax(i)=max(D{1,i}(j,:));
end
dmax(1) = Inf;
[~,r] = sort(dmax,'descend');
temp = D;
for i =2:4
if r(i)~=i
D{1,i}(1,:)=[];
D{1,i}(1,:)=temp{1,r(i)}(j,:);
end
end
end

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 14 Aug 2020
Edited: Bruno Luong on 14 Aug 2020
dmax = cellfun(@max, D);
dmax(1) = Inf;
[~,i] = sort(dmax,'descend');
D = D(i);
  2 Comments
Chandrashekar D S
Chandrashekar D S on 14 Aug 2020
Edited: Chandrashekar D S on 14 Aug 2020
Thanks it is working perfect for if cell array content as only one row
Rik
Rik on 14 Aug 2020
You will have to use something different from Matlab. Arrays in Matlab must be rectangular.
How would you try to adapt this answer to your needs? See also the tips that John gave you in his answer.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 14 Aug 2020
Edited: John D'Errico on 14 Aug 2020
1. Since these are cell arrays, learn to use cellfun. This is a tool that can apply some function to each element of a cell array.
2. Can you write a function that will compute the maximum of a vector? (Hint: you already have one. Give Max a call, and see if he knows the name of that function.)
3. Do you know how to index a cell array, creating a new cell array, so you have extracted all but the first into a new temporary cell array?
4. Can you then compute the maximum of every element of a cell array? (See steps 1 and 2.) I the result of this operation a vector?
5. Can you sort a vector in descending order? If not, then read the help for sort. What is the second output argument from sort? If you don't know how to use sort, then read the help. Read what "doc sort" tells you! Are there examples in there? Look at the examples.
6. Can you use the set of indices, the sort tags that came from the second output of sort, to then sort the cell array from step 3?
7. Finally, stuff the sorted array back into the first, replacing elements 2:end.
I'm sorry if it seems I have acted as if you have not a clue how to use MATLAB, but from your question, you have not bothered to try reading the help for the tools you want to use. And you clearly are not comfortable with cell arrays.
Break large problems into small problems. Solve each sub-problem, if necessary doing the work to learn what you need. This often means reading the help for tools. Here, those tools might be cellfun, max, sort. You also need to learn to index arrays. At every step, dump what you did onto the command window. Did it do what you expected? Try several cases, until you are sure you understand the process. This is how you will learn. TRY IT. Make an effort. If you get stuck, then ask again. If you want, as a comment on my answer. Show what you tried. I'll give you more help if you show an effort.
I'll even get you started.
>> D = {[0 0 1 2 3 ] , [0 0 4 0 0 ], [0 5 6 0 ], [4 0 ]}
D =
1×4 cell array
{1×5 double} {1×5 double} {1×4 double} {1×2 double}
>> D{:}
ans =
0 0 1 2 3
ans =
0 0 4 0 0
ans =
0 5 6 0
ans =
4 0
The second line I wrote extracts the elements of the cell array, putting them into what is called a comma separated list. It is a nice way to display all elements of a cell array in the command window. It can also be used to pass them as separate arguments into a function, though you will not need that capability at all today.
You should try learn to use MATLAB. For example, can you find a way to compute the sum of every cell in that array, returning the result as a simple vector, thus not a cell array? Get your hands dirty. At least your fingertips.
I am serious here. When you cannot solve a problem, you look to see if you can do something to get closer to your goal. Can you find a tool that does what you need? Break the big programming elephant into byte sized chunks. Eat it one byte at a time.
  1 Comment
Chandrashekar D S
Chandrashekar D S on 14 Aug 2020
yeah your right about me. I have started working in matlab recently.defintely i am learning so many things. I have been working on the same function for few days. this was the last part of my function. Here i just try to find the easy way. But your right i will try myself to find the soultion.
Thanks for the tipps and I will try my best :)

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!