Setting element of a variable X to zero with conditional statement, and putting it into new variable Y?

After generating random numbers and putting them in a variable X, how can I give a statement that if element of X is more than 0.3 and less than 0.6, turn it into 0 and put it in a new variable which I will call Y?

Answers (2)

idx = ( x > 0.3 ) & ( x < 0.6 ) ; % index into whichever variables you like and assign the value/values
x(idx) % grabs values of x within the range

2 Comments

Thank you for your quick reply, but I don't quite understand.
Let me explain,
X = randn(1, 10000); % I generated as you can see random numbers and put them in one variable X. And now what I am trying to do is just give a condition to scan elements of variable X and find numbers which are less than 0.6 and more than 0.3 (0.3 > numbers > 0.6), and turn them to 0, and save the numbers which I changed to 0 in a new variable Y.
So, how can I do it? And my professor wants me to write these comman codes using only one line. :-(
What I have shown should get you started , I am not going to finish your homework sorry.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!