Borrar filtros
Borrar filtros

Solving logical expressions with if statement

1 visualización (últimos 30 días)
Asim Ismail
Asim Ismail el 13 de Mayo de 2017
Comentada: Stephen23 el 13 de Mayo de 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.

Respuesta aceptada

Jan
Jan el 13 de Mayo de 2017
Editada: Jan el 13 de Mayo de 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 comentarios
Asim Ismail
Asim Ismail el 13 de Mayo de 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 el 13 de Mayo de 2017
@asim ismail: use indexing.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by