How to remove an element in an array if this element is smaller than 1?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
YZ
el 3 de Sept. de 2014
Comentada: YZ
el 3 de Sept. de 2014
Hi, Everyone,
There are two arrays with the same size. For example: A=[1, 0.5, 2, 0.75, 3, 0.85, 4, 5]; B=[1, 2, 3, 4, 5, 6, 7, 4]. How to remove an element in A if this element is smaller than 1 OR this element is larger than the corresponding element with the same index in B?
I used the following line but it didn't work:
A((A < 1) | | (A > B)) = [];
MATLAB returned the following red lines: "Operands to the and && operators must be convertible to logical scalar values."
The result I want is A=[1, 2, 3, 4].
At the same time I also want to do the same operation to B. If the element in A is smaller than 1, the corresponding element in B will be removed, and if the element in A is larger than the corresponding element in B, that element in B will also be removed. So the resultant B I want is B=[1, 3, 5, 7].
Hope someone can help. Many thanks.
0 comentarios
Respuesta aceptada
Image Analyst
el 3 de Sept. de 2014
Too many |. Try this:
A((A < 1) | (A > B)) = [];
2 comentarios
Joseph Cheng
el 3 de Sept. de 2014
Editada: Joseph Cheng
el 3 de Sept. de 2014
To give a bit more information on the | vs | | and & vs &&. Is that the double symbol version are short circuit versions. The difference is that the second operand (short circuited) is evaluated only if the first operand is not fully determined. so with that said it doesn't really work for arrays as there are multiple true and false results.
- & (AND operator) and | (OR operator) can operate on arrays in an element-by-element-wise fashion.
- && and are short-circuit versions where the second operand is only evaluated if the result is not fully determined by the first operand. These can only operate on scalars, not arrays.
Más respuestas (0)
Ver también
Categorías
Más información sobre Operators and Elementary Operations 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!