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

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!

 採用された回答

Wayne King
Wayne King 2012 年 1 月 28 日

6 投票

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.

3 件のコメント

Image Analyst
Image Analyst 2012 年 9 月 26 日
編集済み: Image Analyst 2012 年 9 月 26 日
It should be noted that the find() is not necessary.
Pierre Lonfat
Pierre Lonfat 2017 年 4 月 20 日
Do not work ! Matrix is not maintained ...
hbabar
hbabar 2017 年 5 月 25 日
Ofcourse if you remove the elements, which was what was asked in the question, the matrix will not be mantained. If you just want to replace those values with a zero just do this:
A.*(A>Threshold))

サインインしてコメントする。

その他の回答 (1 件)

Mohiuddin  Ahmed
Mohiuddin Ahmed 2012 年 9 月 26 日

1 投票

I have a matrix like this : X =
5.1000 3.5000
4.9000 3.0000
4.7000 3.2000
4.6000 3.1000
5.0000 3.6000
5.4000 3.9000
4.6000 3.4000
5.0000 3.4000
4.4000 2.9000
4.9000 3.1000
I would like to delete an element from the matrix. how can I do that ?

3 件のコメント

Image Analyst
Image Analyst 2012 年 9 月 26 日
You can't because the matrix would no longer be rectangular if you deleted just one element. You need to delete a whole row or whole column.
X(rowToDelete,:) = []; X(:, columnToDelete) = [];
Mohiuddin  Ahmed
Mohiuddin Ahmed 2012 年 10 月 31 日
well, if I want to delete an entire row from the matrix X? How can I do that ?
Marco Noll
Marco Noll 2018 年 3 月 20 日
%lets assume we have some time
%and measurement data and want
%to remove any data that is out of range
%create some sample matrix
c= [1:6;100:105]
%create a mask matrix with all 1 where criteria matches
mask = c>102 & c<105
%use the second row of the mask to remove the data columns
c(:,mask(2,:))
%should work the same when transposing it

サインインしてコメントする。

カテゴリ

質問済み:

2012 年 1 月 28 日

コメント済み:

2018 年 3 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by