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 次查看(过去 30 天)
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!

采纳的回答

Rik
Rik 2019-4-2
编辑:Rik 2019-4-2
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by