Created
April 16, 2014 01:14
-
-
Save tonyfast/10795304 to your computer and use it in GitHub Desktop.
A Masked N-D array containing only the first M lowest values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% A code to find the N highest or lowest values in a N-D array without using sort. | |
N = [10 10 10]; | |
% nbins is an approximate | |
% higher values improve accuracy but lower efficiency | |
nbins = 101; | |
chopto = 100; % Chop to the first hundred values approximately | |
A = rand( N ); | |
[y,x] = hist(A(:), nbins); | |
%% the index to chop | |
id = find( cumsum(y) >= chopto, 1, 'first' ); | |
%% logical array into the lower values | |
b = A < x(id); | |
disp(sprintf( 'The number of nonzero values is %i.', nnz(b)) ); | |
%% The new masked array is | |
A(~b) = 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment