user366312
user366312

Reputation: 16894

How can I replace a matrix element using logical indexing?

The following is my matrix,

coords = [
     1    -1;
     1     0;
   219     1;
   219     2;
   219     3;];

.

Suppose, I want to replace elements of the 2nd column, which are less than 1, with 1.

I.e., my expected matrix would be,

coords = [
     1     1;
     1     1;
   219     1;
   219     2;
   219     3;];

So, I tried to do the following,

coords(:,coords(:, 2)<1) = 1;

It is not working.

How to achieve that?

Upvotes: 0

Views: 45

Answers (1)

bla
bla

Reputation: 26069

you almost got it:

 coords(coords(:,2)<1,2)=1;

it is the first entry you want to edit at the 2nd col...

Upvotes: 3

Related Questions