0

suppose I have a Matrix like this:

A=[-3 -4 -5 -6 0 2 3 3 4 5 8 9 10]

Now I want to extract a Matrix whose positive value will be greater than 8 and all other +ve value less than 8 will be zero. And its -ve values will be less than -5 and all other -ve values will be zero. That means something like this:

A= [0 0 0 -6 0 0 0 0 0 0 0 9 10]

How to do that?

I have tried the below things

A(A<8)=0

it gives A=[ 0 0 0 0 0 0 0 0 0 0 8 9 10]

but my negative values are gone.

if i try

A(A>-5)=0

then I get;

A=[ 0 0 -5 -6 0 0 0 0 0 0 0 0 0]

But here all +ve values are gone. plz help..

sifat
  • 5
  • 1
  • 3

2 Answers2

2

You can do

A(A<8 & A>-5) = 0

Also shown in the documentation Find array elements that meet a condition

EDIT: If, as pointed out in the comment, and in how you describe you desired result, you want all values less than or equal to 8 and greater than or equal to -5 to be 0, the answer is

A(A<=8 & A>=-5) = 0
Anders Schou
  • 184
  • 1
  • 13
0

Hy, try using the AND operation

A=[-3 -4 -5 -6 0 2 3 3 4 5 8 9 10];
A(A > -5 & A < 8) = 0;

This would give you the desired result.

Greetz

CodeX
  • 11
  • 2