if r=rand(2,8) . I wanna find every element which is smaller than 0.5 and replace it with any other number say 0 or 1 or 2. how to do it....??

if r=rand(2,8) . I wanna find every element in r which is smaller than 0.5 and replace it with any other number; say 0 or 1 or 2. how to do it....??

Answers (1)

r(r<0.5)=0

3 Comments

If you want, but it's unnecessary
r=rand(2,8)
idx=find(r<0.5)
r(idx)=0
IF you need you could find even more particular values using logical AND & (here && since scalar) or logical OR |
r(0.3<r && r<0.5) = 59
r(0.2>r || r>0.99) = 13
such as the above. Good Luck.

This question is closed.

Asked:

on 9 Nov 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!