Finding min values in structure array
18 ビュー (過去 30 日間)
古いコメントを表示
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
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
>>
採用された回答
その他の回答 (2 件)
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);
0 件のコメント
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);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!