Finding the maximum in a structure array

How would I go about finding the maximum in a structure array as well as the corresponding data with that maximum value. For example, if the structure array provided data about the name, gender, and quiz grade, how would I found the maximum (highest) quiz grade and display the name and gender corresponding to that quiz grade?

 채택된 답변

Image Analyst
Image Analyst 2022년 2월 24일

0 개 추천

If strArray is your structure array (one structure per student), and quizGrade is the value for each structure, then to get all the grades for all the students into one vector you can do
allGrades = [strArray.quizGrade];
[maxGrade, indexOfMax] = max(allGrades);
studentWithHighestGrade = strArray(indexOfMax).studentName

댓글 수: 2

Cam B
Cam B 2022년 2월 25일
Thank you. Is there a way to place all the data that is less than the maximum into a new structure?
Yes
structsToExtract = allGrades <= someValue; % someValue is less than maxGrade of course, otherwise you'd select everything.
newStruct = strArray(structsToExtract);

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

추가 답변 (0개)

카테고리

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

질문:

2022년 2월 24일

댓글:

2022년 2월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by