Solving logical expressions with if statement

2 views (last 30 days)
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.

Accepted Answer

Jan
Jan on 13 May 2017
Edited: Jan on 13 May 2017
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 Comments
Asim Ismail
Asim Ismail on 13 May 2017
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?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!