Finding min and max values in a structure
6 次查看(过去 30 天)
显示 更早的评论
For a 1x1 structure with multiple fields, each field consisting of a cell of numbers, how can you find the minimum and maximum values over the entire structure?
0 个评论
回答(3 个)
David Hill
2022-11-28
d=struct2cell(yourStruct);
m=0;M=0;
for k=1:length(d)
m=min([m,d{k}]);
M=max([M,d{k}]);
end
Matt J
2022-11-28
编辑:Matt J
2022-11-28
each field consisting of a cell of numbers
If you really do mean a cell array, that seems like an inadvisable organization of the data. You should probably have each field be a numeric matrix.
yourStruct.a=[1,2];
yourStruct.b=[3 4;
5 6];
Max = max( structfun(@(z)max(z,[],'all'), yourStruct) )
Min = min( structfun(@(z)min(z,[],'all'), yourStruct) )
0 个评论
Voss
2022-11-28
编辑:Voss
2022-11-28
"a 1x1 structure with multiple fields, each field consisting of a cell of numbers"
yourStruct.a={1,2};
yourStruct.b={3 4;
5 6};
Max = max( structfun(@(z)max([z{:}],[],'all'), yourStruct) )
Min = min( structfun(@(z)min([z{:}],[],'all'), yourStruct) )
Another way:
d = struct2cell(yourStruct);
m = NaN;
M = NaN;
for k = 1:numel(d)
m = min([m,d{k}{:}]);
M = max([M,d{k}{:}]);
end
disp(M);
disp(m);
1 个评论
Voss
2022-11-28
@Sean de Wolski: This answer is not a copy of the other answers. This answer uses the methods proposed by the other answers, adapted to work for a structure of cell arrays of numbers (as opposed to a structure of numeric arrays, which the other answers work for), which is the description given in the question, and which neither other answer works for.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!