필터 지우기
필터 지우기

Solving logical expressions with if statement

조회 수: 1 (최근 30일)
Asim Ismail
Asim Ismail 2017년 5월 13일
댓글: Stephen23 2017년 5월 13일
I am solving some conditions having logical expressions with 'if'statement, but these conditions are little complex, so using if statement every time will make it more complicated to understand. So can someone please suggest me another way to solve it?
a = rand(10,2);
b = [1 4 7 10];
Condition:
if any number from first column of 'a' is less than or equal to the first column of 'b' and less than second column of 'b', and if any number from second column of 'a' is greater than or equal to the first column of 'b' and less than second column of 'b', then import these numbers from matrix 'a' and store them in a new matrix (lets say 'result').
a(:,1) >= b(1,1) and < b(1,2) and if a(:,2) >= b(1,1) and < b(1,2)
I have 8 more conditions like this.

채택된 답변

Jan
Jan 2017년 5월 13일
편집: Jan 2017년 5월 13일
Then the 3rd and 4th column of b are not used?
a = rand(10,2);
b = [1 4 7 10];
Index = (a(:,1) >= b(1,1) & a(:,1) < b(1,2) & ...
a(:,2) >= b(1,1) & a(:,2) < b(1,2));
Result = a(Index, :);
Or:
Index = all(a(:,1:2) >= b(1,1) & a(:,1:2) < b(1,2), 2);
  댓글 수: 3
Asim Ismail
Asim Ismail 2017년 5월 13일
ofcourse the 3rd and 4th column of b will be used. This one was for 1st and 2nd column, later it will be for 2nd and 3rd column, and so on...
Can it be a general formula?
Stephen23
Stephen23 2017년 5월 13일
@asim ismail: use indexing.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by