Creating a script with if statements

1 回表示 (過去 30 日間)
Cside
Cside 2019 年 8 月 30 日
コメント済み: Cside 2019 年 9 月 2 日
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 件のコメント
Guillaume
Guillaume 2019 年 8 月 30 日
fixed. Thanks!
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 Center および File ExchangeTime Series Events についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by