Creating a script with if statements

1 visualización (últimos 30 días)
Cside
Cside el 30 de Ag. de 2019
Comentada: Cside el 2 de Sept. de 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!

Respuesta aceptada

Guillaume
Guillaume el 30 de Ag. de 2019
Editada: Guillaume el 30 de Ag. de 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 comentarios
Guillaume
Guillaume el 30 de Ag. de 2019
fixed. Thanks!
Cside
Cside el 2 de Sept. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Time Series Events en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by