How to Remove Elements in a Matrix Less than a Threshold?

Juan Miguel asked on 28 Jan 2012 at 11:47
Latest activity: Answer by Wayne King on 28 Jan 2012 at 12:21

Hi,

I have a 3000 x 3000 matrix and want to remove all elements inside the matrix that are less than a specific value, .5 for example.

How would I go about this?

Here's what I have so far but it doesn't seem to work:

function y = highestValues(a,b)
% a is the matrix
% b is the threshold value
[sortedValues,~] = sort(a,1,'descend');
exclude = sortedValues < b;
sortedValues(exclude) = [];
highestValue = sortedValues;
clearvars n sortedValues;
y = highestValue;

Thank you!

0 comments

Products

    1 answer

    Wayne King answered on 28 Jan 2012 at 12:21
    Accepted answer

    You can just do this: (removing all entries larger in absolute value than 2)

    x = randn(10,10);
    indices = find(abs(x)>2);
    x(indices) = [];
    

    But then x will be a vector and no longer a matrix of the same size you started with:

    You can also do this:

        x = randn(10,10);
        indices = find(abs(x)>2);
        x(indices) = NaN;  

    This will maintain your matrix.

    0 comments

    Contact us at files@mathworks.com