Sort raw data from a stream, with more zeros on top

I have raw data from a stream, the data is represented like the following N*13 matrix:
% Z Data dE
% _____________ ___________ _____
% 0 0 0 0 0 0 1 2 3 5 2 6 1.0
% 2 1 0 9 0 0 2 5 9 1 8 6 1.5
% 0 0 4 0 0 0 1 2 2 5 9 4 2.0
% 0 0 7 0 1 6 1 6 3 5 1 6 0.1
% 0 0 3 0 1 6 1 2 3 5 7 6 0.4
Rows for whole matrix must be sorted like this:
  1. Priority to Z, by number of zeros in a descending order (more zeros on top)
  2. If two lines have same number of zeros in Z, lowest dE has priority (on top)
Considering the example above order should be: 1,3,4,5,2

 Accepted Answer

numzeros = sum(yourmatrix(:, 1:6) == 0, 2);
[~, order] = sortrows([numzeros, yourmatrix(:, 13)], [-1 2]);
yourmatrix = yourmatrix(order, :)

More Answers (1)

>> [~,ix]=sortrows([sum(Z==0,2) dE],[-1 2])
ix =
1
3
4
5
2
>>

Categories

Asked:

on 25 Apr 2018

Answered:

dpb
on 25 Apr 2018

Community Treasure Hunt

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

Start Hunting!