Mattia Piron
Mattia Piron

Reputation: 53

Substitute element on a octave matrices

I want to substitute all the element in a matrices comprises between a range. I try to use this method (just simple code for exhample):

A = rand(5);
A(A>0 && A<0.5) = 0.5;

But didn't work. This one work:

A = rand(5);
for j = 1:5
  for i = 1:5
    if A(i,j)>0 && A(i,j)<0.5
      A(i,j) = 0.5;
    endif
  endfor
endfor

But it is very slow.

Upvotes: 2

Views: 82

Answers (1)

Andy
Andy

Reputation: 8091

Almost there:

A = rand(5);
A(A>0 & A<0.5) = 0.5;

See Element-by-element boolean operator. You've used && which is a Short-circuit Boolean Operator.

Upvotes: 1

Related Questions