Finding min values in structure array

12 visualizaciones (últimos 30 días)
BlkHoleSun
BlkHoleSun el 9 de Oct. de 2017
Respondida: BlkHoleSun el 21 de Oct. de 2017
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 comentarios
per isakson
per isakson el 10 de Oct. de 2017
Editada: per isakson el 10 de Oct. de 2017
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 el 10 de Oct. de 2017
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!

Iniciar sesión para comentar.

Respuesta aceptada

BlkHoleSun
BlkHoleSun el 21 de Oct. de 2017
[s.age] ans = 29 7 6 4 2 >> [val,ix] = min([s.age]) val = 2 ix = 5

Más respuestas (2)

Nicolas Schmit
Nicolas Schmit el 10 de Oct. de 2017
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 el 10 de Oct. de 2017
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);

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by