MATLAB Answers


condition checking in matlab

Asked by subha
on 3 Nov 2013
Latest activity Commented on by subha
on 3 Nov 2013

i want to verify each element in the matrix and keep that value in the same place if its true otherwise i want to make it as 0.

example: say x=[0.1 0.2 0.3;0.4 0.5 0.6;0.7 0.8 0.9]

i want to check two condition 1.Y=x>0 &x<0.25 2.Z=x>0.25 & x<0.5

i want both Y and Z to be in same,want to add zeros in all the other locations where condition is not satisfied.Can anybody help?

i used ind=find(x>0)&(x<0.25) and then y(ind). but it produces an to get matrix of size x with added zero in remaining all places.



No products are associated with this question.

2 Answers

Answer by the cyclist
on 3 Nov 2013
 Accepted answer

Here's one way:

ind = (x>0)&(x<0.25);
y = ind.*x;

  1 Comment

on 3 Nov 2013


Answer by Azzi Abdelmalek
on 3 Nov 2013

x=[0.1 0.2 0.3;0.4 0.5 0.6;0.7 0.8 0.9]
idy=x>0 & x<0.25
idz=x>0.25 & x<0.5

  1 Comment

on 3 Nov 2013

thanks all for your valuable reply

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!