Effacer les filtres
Effacer les filtres

Solving logical expressions with if statement

1 vue (au cours des 30 derniers jours)
Asim Ismail
Asim Ismail le 13 Mai 2017
Commenté : Stephen23 le 13 Mai 2017
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.

Réponse acceptée

Jan
Jan le 13 Mai 2017
Modifié(e) : Jan le 13 Mai 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 commentaires
Asim Ismail
Asim Ismail le 13 Mai 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?
Stephen23
Stephen23 le 13 Mai 2017
@asim ismail: use indexing.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by