Reputation: 225
Say I have a row:
NaN 29.99 30.00 NaN 24.32 NaN ...............
How can I sort the data in descending order and select the top 10% largest number?
The 'sort' function will put NaN as the "largest"~~~ how can I exclude it?
Thanks!!
Upvotes: 3
Views: 4545
Reputation: 855
[~, iSort] = sort(x, 'descend'); % Sort x including NaNs
iSort(isnan(x(iSort))) = []; % Now remove indices to sorted x which are NaNs
Upvotes: 0
Reputation: 19880
If you just need to select the top 10% largest numbers you can use QUANTILE or PRCTILE functions from Statistical Toolbox (no sort needed):
x_largest = x(x >= quantile(x, 0.9));
or
x_largest = x(x >= prctile(x, 90));
Those functions don't take NaNs into account.
Upvotes: 1
Reputation: 12737
You can first sort the row, and then exclude the NaN's
sorted_row = sorted_row( ~isnan(sorted_row) );
This will remove all NaNs from the sorted row.
Better Yet, to save computations , you should exclude NaNs before you sort.
sorted_row = sort( row(~isnan(row)) );
Upvotes: 2