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

16 ビュー (過去 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
  2 件のコメント
Guillaume
Guillaume 2019 年 4 月 2 日
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)
Yurii Iotov
Yurii Iotov 2019 年 4 月 2 日
Thanks! Solution is easy but beautiful!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops 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