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

28 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

Rik
Rik le 2 Avr 2019
Modifié(e) : Rik le 2 Avr 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 commentaires
Guillaume
Guillaume le 2 Avr 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)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by