Finding the maximum in a structure array

4 Ansichten (letzte 30 Tage)
Cam B
Cam B am 24 Feb. 2022
Kommentiert: Image Analyst am 25 Feb. 2022
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?

Akzeptierte Antwort

Image Analyst
Image Analyst am 24 Feb. 2022
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 Kommentare
Cam B
Cam B am 25 Feb. 2022
Thank you. Is there a way to place all the data that is less than the maximum into a new structure?
Image Analyst
Image Analyst am 25 Feb. 2022
Yes
structsToExtract = allGrades <= someValue; % someValue is less than maxGrade of course, otherwise you'd select everything.
newStruct = strArray(structsToExtract);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by