# Finding min and max values in a structure

조회 수: 9(최근 30일)
L'O.G. 2022년 11월 28일
댓글: Voss 2022년 11월 28일
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?

댓글을 달려면 로그인하십시오.

### 답변(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
##### 댓글 수: 1표시 없음숨기기 없음
L'O.G. 2022년 11월 28일
I get an error for both lines in the loop:
`Invalid data type. First argument must be numeric or logical.`

댓글을 달려면 로그인하십시오.

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) )
Max = 6
Min = min( structfun(@(z)min(z,[],'all'), yourStruct) )
Min = 1
##### 댓글 수: 0표시 이전 댓글 수: -1숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

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) )
Max = 6
Min = min( structfun(@(z)min([z{:}],[],'all'), yourStruct) )
Min = 1
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);
6
disp(m);
1
##### 댓글 수: 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.

댓글을 달려면 로그인하십시오.

### 범주

Find more on Structures in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!