How to arrange a matrix in descending order w.r.t rows?

13 views (last 30 days)
If we have a large matrix and we want to arrange it in descending order i.e., the largest row shoud come on top, then 2nd largest row comes as 2nd row, then 3rd largest as 3rd row and so on but the columns are not disturbed. I do it like this:
clear all; clc
a=[1 2 3 4;1.1 2.1 3.1 4.1;1.2 0 3.2 4.2];
b=sort(a,"descend");
[a b]
But in this the columns are also disturbed.

Answers (2)

the cyclist
the cyclist on 13 Jan 2023
Edited: the cyclist on 13 Jan 2023
Probably use the sortrows function. I'd be more specific, but it is unclear to me what you mean by "largest row". For example, which row is larger?
M = [2 7 11 19;
3 5 13 17];
  3 Comments
the cyclist
the cyclist on 13 Jan 2023
I'm not sure I understand. I ran your code here (and wrote a in a way that makes the rows clearer).
Each row stays intact, but is moved up or down, based on the sorting you did. This isn't what you want? Maybe you could write out the output you expected to get?
fval=[2.11 2.10 2.13 2.18 2.09];
a=[1 2 3 4;
1.1 2.1 3.1 4.1;
1.2 0 3.2 4.2;
1.01 2.01 3.01 4.01;
1.1 2.9 3.1 4.2];
[fval1 ind]=sort(fval,'descend');
b=a(ind,:)
b = 5×4
1.0100 2.0100 3.0100 4.0100 1.2000 0 3.2000 4.2000 1.0000 2.0000 3.0000 4.0000 1.1000 2.1000 3.1000 4.1000 1.1000 2.9000 3.1000 4.2000
Sadiq Akbar
Sadiq Akbar on 13 Jan 2023
Thanks a lot for your kind response. May be I am unable to communicate my question properly. Look I run this code:
clear all; clc
fval=[2.11 2.10 2.13 2.18 2.09];
a=[1 2 3 4;
1.1 2.1 3.1 4.1;
1.2 0 3.2 4.2;
1.01 2.01 3.01 4.01;
1.1 2.9 3.1 4.2];
[fval1 ind]=sort(fval,'descend');
b=a(ind,:);
for ii=1:length(a)
Na(ii)=norm(a(ii,:));
Nb(ii)=norm(b(ii,:));
end
Both=[Na' Nb']
Now when we look at the 2nd column of Both, the values in that column are not in the descending order. I want to make them in descending order and then arrange the matrix 'a' accordingly and then find the row which corresponds to the minimum value of Nb. But I am confused.

Sign in to comment.


the cyclist
the cyclist on 13 Jan 2023
Answering here, based on your comment to my other answer.
It seems that you just want to sort the matrix a according to its norm. It also seems that the vector fval has absolutely nothing to do with the norm. So why are you sorting by fval?
This code will create the matrix b, which it seems is what you want.
% Original matrix
a = [1 2 3 4;
1.1 2.1 3.1 4.1;
1.2 0 3.2 4.2;
1.01 2.01 3.01 4.01;
1.1 2.9 3.1 4.2];
% Calculate the norm of each row of a
for ii = 1:height(a)
Na(ii) = norm(a(ii,:));
end
% Find the sorting order index
[~, ind]=sort(Na,'descend');
% Define the matrix b, which is a sorted by the norm
b = a(ind,:);
% Calculate the norm of each row of b
for ii = 1:height(a)
Nb(ii) = norm(b(ii,:));
end
% Show that b is sorted by the norm
Both = [Na' Nb']
Both = 5×2
5.4772 6.0721 5.6604 5.6604 5.4148 5.4955 5.4955 5.4772 6.0721 5.4148
  1 Comment
Sadiq Akbar
Sadiq Akbar on 14 Jan 2023
I tried to modify my code according to yours but I am stuck. I am going to share my data here along with the code. The data is in the attachment and the code is below:
clear all; clc
load 2sn0dB
fval=one;
Error=u-two;% two is my matrix b
for ii=1:length(Error)
NError(ii)=norm(Error(ii,:));%Norm of the error
end
% Arranging the column vector 'NError' in descending order
[NError,ind]=sort(NError','descend');
% Arranging matrix two according to indices of NError
two2=two(ind,:);
In this two is my matrix b of size 100 x 4. I want to find the error between each of its row with vector u. Then I want to find the norm of each row of matrix Error. After that I want to arrange those norms into descending order. Then I want to arrange the matrix two according to the indices of those sorted norms. Then I want to decrease the error between u and matrix two but how? And then I want to update the matrix two such that their values do not deviate too much from u. But I don't know whether I am doing it correctly or wrongly?

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!