Finding min values in structure array

조회 수: 14 (최근 30일)
BlkHoleSun
BlkHoleSun 2017년 10월 9일
답변: BlkHoleSun 2017년 10월 21일
Below is my current structure array, and I would like to be able to search the structure and find the minimum age.
field1='relation';
value1={'spouse', 'son', 'son', 'daughter', 'daughter'};
field2='name';
value2={'Rebekah', 'Caleb', 'Isaac', 'Faith', 'Annabel'};
field3='state';
value3={'OH', 'VA', 'VA', 'VA', 'VA'};
field4='age';
value4={'29','7','6','4','2'};
s = struct(field1,value1,field2,value2,field3,value3,field4,value4);
  댓글 수: 2
per isakson
per isakson 2017년 10월 10일
편집: per isakson 2017년 10월 10일
Are the values of age a mistake? They are character strings,
value4={'29','7','6','4','2'};
I replaced the assignment by
value4={29,7,6,4,2};
Then finding min is simple
>> [s.age]
ans =
29 7 6 4 2
>> [val,ix] = min([s.age])
val =
2
ix =
5
>>
BlkHoleSun
BlkHoleSun 2017년 10월 10일
yes, it was a mistake. thank you. [val,ix] = min([s.age]); s(ix) now I get all the info from the min age. Thanks again!

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

채택된 답변

BlkHoleSun
BlkHoleSun 2017년 10월 21일
[s.age] ans = 29 7 6 4 2 >> [val,ix] = min([s.age]) val = 2 ix = 5

추가 답변 (2개)

Nicolas Schmit
Nicolas Schmit 2017년 10월 10일
First, convert the age data from string to numeric, and concatenate it into an array.
ageArray = arrayfun(@(x) str2double(x.age), s);
Now, find the index of the minimum age.
[~, iAgeMin] = min(ageArray);

Jan
Jan 2017년 10월 10일
This struct is impractical for searching, as well as it is to search for a minimum in values stored as strings. But it works:
num = str2double({s.age});
minValue = min(num);

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by