Gjorgji
Gjorgji

Reputation: 23094

Matlab: Applying threshold to one dimension in a matrix

I have a matrix M(x,y). I want to apply a threshold in all values in x, such that if x

Example:

M = 1, 2; 3, 4; 5, 6;

If t = 5 is applied on the 1st dimension, the result will be

R = 0, 2; 0, 4; 5, 6;

Upvotes: 3

Views: 8034

Answers (2)

eqzx
eqzx

Reputation: 5559

To do it in a matrix of arbitrary dimensions:

thresh_min = 5;
M(M < thresh_min) = 0;

The statement M < thresh_min returns indices of M that are less than thresh_min. Then, reindexing into M with these indices, you can set all of these valuse fitting your desired criterion to 0 (or whatever else).

Upvotes: 1

mathematical.coffee
mathematical.coffee

Reputation: 56905

One way (use M(:,1) to select the first column; M(:,1)<5 returns row indices for items in the first column that are lest than 5))-

> R = M;
> R(M(:,1)<5,1) = 0

R =

   0   2
   0   4
   5   6

Another -

R = M;
[i,j]=find(M(:,1)<5); % locate rows (i) and cols (j) where M(:,1) < 5
                      % so j is just going to be all 1
                      % and i has corresponding rows
R(i,1)=0;

Upvotes: 5

Related Questions