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

1 次查看(过去 30 天)
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?

回答(1 个)

Ameer Hamza
Ameer Hamza 2020-6-11
Try this
VxMax = [flow{:}];
VxMax = max([VxMax.Vx]);
or alternatively
VxMax = max(cellfun(@(x) x.Vx, flow), [], 'all');
  5 个评论
Ameer Hamza
Ameer Hamza 2020-6-11
编辑:Ameer Hamza 2020-6-11
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by