Find Max of Array of structures.

I have a 1x27 structure with five fields. I Need to calculate the country with the most gold medals. so I need to find the max of all the elements in the gold field. How do I do this. Thanks. My structure looks like: Countries, gold , silver, bronze
"AUS" 2 1 0
"AUT" 4 6 6
"BLR" 1 1 1
"CAN" 14 7 5

답변 (2개)

Stephen23
Stephen23 2020년 3월 3일
편집: Stephen23 2020년 3월 3일

0 개 추천

Where S is your structure:
[cnt,idx] = max([S.gold]);
S(idx).Countries
A comma-separated list is used to concatenate all of the gold values into one vector:

댓글 수: 2

Arno Claassens
Arno Claassens 2020년 3월 3일
thanks that helps a lot
Stephen23
Stephen23 2020년 3월 4일
@Arno Claassens: please remember to accept the answer that helped you most!

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

Steven Lord
Steven Lord 2020년 3월 3일

0 개 추천

Rather than storing your data in a struct array I'd consider storing it in a table array. The struct2table function may be able to help you convert your data into a table. If you made a table named medals with a variable named Gold then something like this should work:
maxGoldMedals = max(medals.Gold)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

태그

질문:

2020년 3월 3일

댓글:

2020년 3월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by