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 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

Rik
Rik am 2 Apr. 2019
Bearbeitet: Rik am 2 Apr. 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

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by