How to create another vector based on some if condition?

19 vues (au cours des 30 derniers jours)
Elian Ricardo Gonzalez Beltran
Modifié(e) : Guillaume le 26 Nov 2019
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

Réponse acceptée

Guillaume
Guillaume le 26 Nov 2019
Modifié(e) : Guillaume le 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.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by