MATLAB Answers


Sorting matrix based on the sum of the rows

Asked by Eunan McShane on 9 Jul 2019
Latest activity Commented on by Eunan McShane on 9 Jul 2019
I am trying to sort the matrix 'counts' (attached as csv) so that the sum of the rows increases to the 512th pixel so that I have effectively lowest sum to highest sum in order
Thank you


Sign in to comment.




2 Answers

Answer by Akira Agata
on 9 Jul 2019
 Accepted Answer

Like this?
% Read data from CSV
A = csvread('Counts.csv');
% Calculate order vector (pt) based on sum(A,2)
[~,pt] = sort(sum(A,2));
% Sort the matrix
B = A(pt,:);

  1 Comment

Perfect, thank you!

Sign in to comment.

Answer by Bob Nbob
on 9 Jul 2019

A = randi(100, 10, 10);
sums = sum(A,2);
[~,I] = sort(sums);
B = A(I',:);
There may be a better way of doing this, but this is the first thing that came to mind. Just replace A with your data.

  1 Comment

This works fine, although I think Akira's solution is cleaner, thank you though!

Sign in to comment.