Creating a script with if statements

1 次查看(过去 30 天)
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!

采纳的回答

Guillaume
Guillaume 2019-8-30
编辑:Guillaume 2019-8-30
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 个评论
Cside
Cside 2019-9-2
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Time Series Events 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by