How to sort chunks of data file in descending order

2 visualizaciones (últimos 30 días)
Nick Elias
Nick Elias el 31 de Mzo. de 2021
Editada: KSSV el 31 de Mzo. de 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!

Respuesta aceptada

KSSV
KSSV el 31 de Mzo. de 2021
Read the data using readtable and then use function sort.
  2 comentarios
Nick Elias
Nick Elias el 31 de Mzo. de 2021
Editada: Nick Elias el 31 de Mzo. de 2021
I tried this:
data=xlsread('Data.xlsx');
[rows, columns] = size(data);
sorted_X = reshape(sort(data(:), 'descend'), [columns, rows])';
But the output is just the whole column ranked in descending order. I need to rank every chunk of nonzero numbers separately and leave zeros in between.
KSSV
KSSV el 31 de Mzo. de 2021
Editada: KSSV el 31 de Mzo. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by