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

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)

Try this
VxMax = [flow{:}];
VxMax = max([VxMax.Vx]);
or alternatively
VxMax = max(cellfun(@(x) x.Vx, flow), [], 'all');

5 Comments

1) VxMaxx = [Flow_PlaneA{:}]; VxMaxx = max([VxMaxx.Vx]); returns a 160400 element vector not a single number like I would expect
2) K>> VxMax = max(cellfun(@(x) x.Vx, Flow_PlaneA), [], 'all');
Error using cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
3) attempting to fix 2) K>> VxMax = max(cellfun(@(x) x.Vx, Flow_PlaneA,'UniformOutput',false),[], 'all');
Error using max
Invalid data type. First argument must be numeric or logical.
I'm a little lost
Is the 'all' dimension flag not supported in Matlab 2018a?
below appears to be the correct cellfun formatting
K>> VxMax = cellfun(@(x) max(x.Vx,[],'omitnan','all'), Flow_PlaneA,'UniformOutput',false);
Error using max
Dimension argument must be a positive integer scalar within indexing range.
This formulation seems to match my double loop method answer:
VxMax = max(cell2mat(cellfun(@(x) max(max(x.Vx)), Flow_PlaneA,'UniformOutput',false)))
Erik, the problem happened with the code in my answer because the arrangement of data is not obvious. Can you attach the variable Flow_PlaneA in a .mat file?
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.

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Asked:

on 10 Jun 2020

Edited:

on 11 Jun 2020

Community Treasure Hunt

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

Start Hunting!