MATLAB Answers

How to create another vector based on some if condition?

2 views (last 30 days)
I have a 2*1 vector of values
A = [500
200]
condition between or equal to 300 and 500.
Output should be.
B = [true
false];
Any simplest way without using loop. Any help is appreciated. Thanks

  0 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 26 Nov 2019
Edited: Guillaume on 26 Nov 2019
It's rare to have to use if in matlab, and it's certainly not the route to go if you want to operate on whole vectors or matrices at once since if only work with scalar expression. If you were to use if, you'd have to loop over your array.
Your A is trivially transformed into B with:
B = A >= 300 & A <= 500
No if or loop needed.

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.