MATLAB: How to Remove Elements in a Matrix Less than a Threshold

remove elements

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!

Best 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.