MATLAB Answers

How to sort chunks of data file in descending order

2 views (last 30 days)
Nick Elias
Nick Elias on 31 Mar 2021
Edited: KSSV on 31 Mar 2021
Hello,
I have the attached data file (one column) where there are chunks of numbers separated by rows of zeros. I would like to know a quick way to sort every chunk of non zero numbers in descending order.
Thanks!

Accepted Answer

KSSV
KSSV on 31 Mar 2021
Read the data using readtable and then use function sort.
  2 Comments
KSSV
KSSV on 31 Mar 2021
data=xlsread('Data.xlsx');
A = data' ;
ii = zeros(size(A));
jj = abs(A) > 0;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
c = unique(idx) ;
iwant = A' ;
for i = 2:length(idx)
iwant(idx==i) = sort(A(idx==i),'descend') ;
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by