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.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 14 Mär. 2016

0 Stimmen

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

3 Kommentare

Ben Modra
Ben Modra am 14 Mär. 2016
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);
Ben Modra
Ben Modra am 14 Mär. 2016
great, thanks Walter!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by