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

3 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];
[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];
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');
for ii=1:length(a)
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,:));
% 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,:));
% 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
Error=u-two;% two is my matrix b
for ii=1:length(Error)
NError(ii)=norm(Error(ii,:));%Norm of the error
% Arranging the column vector 'NError' in descending order
% Arranging matrix two according to indices of NError
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.


Find more on Operating on Diagonal 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!

Translated by