Creating a script with if statements

1 visualizzazione (ultimi 30 giorni)
Cside
Cside il 30 Ago 2019
Commentato: Cside il 2 Set 2019
Hello. I would like to create a script but am still a little new to flow control. I have a matrix A of numbers (100 x 2) , of which I would like to sort them according to whether they fulfill the criteria of having x in column 1 and y in column 2.
So I have this now:
x = 2:4 & y= 2:4
if A(: , 1) == x & A(:,2) ==y;
I would like the script to create a logical matrix as answer for this, should end up with 9 of them.
Thank you!

Risposta accettata

Guillaume
Guillaume il 30 Ago 2019
Modificato: Guillaume il 30 Ago 2019
In matlab, if statements should be your last resort. By necessity if statements apply to scalars so they tend to force you to use loops.
I'm not very clear on what you're trying to achieve, if you're trying to find all rows of a matrix whose 1st column values re in set x and 2nd column values are in set y, you'd use:
isinset = ismember(A(:, 1), x) & ismember(A(:, 2), y)
If x and y are equal, you could simplify the above to:
isinset = all(ismember(A, [2, 3, 4]), 2)
As you can see, no loop or if required.
  3 Commenti
Guillaume
Guillaume il 30 Ago 2019
fixed. Thanks!
Cside
Cside il 2 Set 2019
ah i see, i think rather than creating a script, creating a function will be more ideal for me. Accepted the answer for the new knowledge learnt :) thank you

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Time Series Events in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by