필터 지우기
필터 지우기

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 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