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?
댓글 수: 0
답변 (1개)
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
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 Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!