Indexing question for member variables of opticalFlow collected into an MxN cell array

1 view (last 30 days)
I am attempting to find the maximum value over alll member variables (PxQ) for a multidimensional set of MxN opticalFlow system objects
Flow_PlaneA{TestNum,frame} loads up a single opticalFlow system object from a mat file
I would like to find max value of all something like:
VxMax = max(Flow_PlaneA{:,:}.Vx
VyMax = max(Flow_PlaneA{:,:}.Vy
Magnitude Max = max(Flow_PlaneA{:,:}.Magnitude
OrientationMax = max(Flow_PlaneA{:,:}.Orientation
This gives me an error for the VxMax equation for example:
"Expected one output from a curly brace or dot indexing expression, but there were " N "results." (where N = 802 for this data)
The only way I can seem to index is to do 2 nested loops for each member variable for example
for TestNum = 1:M
for frame = 1:N
VxMaxNew = max(max(Flow_PlaneA{TestNum,frame}.Vx )); % max(max( covers PxQ)
VxMax = max([VxMaxNew VxMax])
end
end
Is there an array type indexing based method to do this I am just missing? How do I handle the mix of opticalFlow objects (which look like structs to me) and the multidimensional cellarray indexing at the same time? Is that possible?

Answers (1)

Ameer Hamza
Ameer Hamza on 11 Jun 2020
Try this
VxMax = [flow{:}];
VxMax = max([VxMax.Vx]);
or alternatively
VxMax = max(cellfun(@(x) x.Vx, flow), [], 'all');
  5 Comments
Ameer Hamza
Ameer Hamza on 11 Jun 2020
Edited: Ameer Hamza on 11 Jun 2020
The solution could have been more efficient, but some of the cells are empty. The solution requires the creation of an intermediate variable. Try the following two alternatives
maxVx = max(arrayfun(@(x) max(max(x.Vx)), [Flow_PlaneA{:}]));
or
maxVx = max(max(cellfun(@helperFun, Flow_PlaneA)));
function y = helperFun(x)
if ~isempty(x)
y = max(max(x.Vx));
else
y = -inf; % smallest value
end
end
'all' is not supported in R2018a, so two calls to max() are required.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!