logical on structure data

I need to perform a logical operation on array data in a structure, I'm close but cant quite find the neat & efficient solution.
The structure is:
s = struct('binarydata',zeros(vidHeight,vidWidth), 'otherStuff', other);
%then populate s in a loop
...
I then want to perform logical operation on the data in parts of s, eg elementwise and across a range of arrays in s, like this:
result = and ( s(1).binarydata, s(2).binarydata,s(3).binarydata, ...);
but I'd like to be able to do dynamically in a loop, eg:
result = and ( s(i:i+60).binarydata);
which doesn't work as s(i:i+60).binarydata only returns the 60th element. or
result = and ( [s(i:i+60).binarydata]);
which doesn't work either because it concatenates the arrays.
I'm close but can't quite get the syntax right.

 采纳的回答

dimno = 1 + ndims( s(1).binarydata );
result = all( cat(dimno, s(i:i+60).binarydata ), dimno);

3 个评论

Thanks Walter. That works for and(), but how about more generically for or() sum() max() etc. I have a feeling handles apply here but I'm not that familiar with them.
For "or":
result = any( cat(dimno, s(i:i+60).binarydata ), dimno);
for "max":
result = max( cat(dimno, s(i:i+60).binarydata ), dimno);
for "sum":
result = sum( cat(dimno, s(i:i+60).binarydata ), dimno);
great, thanks Walter!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Structures 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by