Solving logical expressions with if statement

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

请先登录,再进行评论。

更多回答(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