How to unit (combine) logical array "along" it's 3rd dimension or How to find logical OR betwen all pages along 3rd dim of 3D array

17 visualizaciones (últimos 30 días)
Hi
I have a function with an input N. After all the calculations inside this function I have an array with dimensions (x,y,N). Each page in a 3d dimension of this array has logical values (1 or 0).
I need to unit (combine) logical array "along" it's 3rd dimension or to find logical OR betwen all pages along 3rd dim of 3D array. Each time N is varying and I am doing all my calculations inside a loop 1:N.
At the end of the loop I need combined 2D array with logical values. At the moment I am in trap how to do it
Thanks!

Respuesta aceptada

Rik
Rik el 2 de Abr. de 2019
Editada: Rik el 2 de Abr. de 2019
As Guillaume mentions, you should be using this:
any(L,3)
instead of my original answer
sum(L,3)>0
(Even if the result should be identical, there are some subtle differences, especially for edge cases.)
The reason is that Matlab might do some optimization, because it doesn't have to check all pages if the first already is true. This short-circuit evaluation has the potential of speeding up your code, especially for larger arrays. I don't know in which step sum will convert your array to a double, but it does somewhere along the line, which might tip you over the edge of needed memory for very large arrays.
original answer (to keep the record):
sum(L,3)>0
  2 comentarios
Guillaume
Guillaume el 2 de Abr. de 2019
A logical OR is more properly implemented as
any(L, 3)
Both will produce the same result.
And for the record, a logical AND would be
all(L, 3)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by