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

1 回表示 (過去 30 日間)
Erick Oberstar
Erick Oberstar 2020 年 6 月 10 日
編集済み: Ameer Hamza 2020 年 6 月 11 日
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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by