MATLAB Answers

find max value of structure

18 views (last 30 days)
FV
FV on 5 May 2020
Commented: Image Analyst on 5 May 2020
I have a small problem today.
I have gotten a struct that's 1x30 and it's just 30 values in the one column. It shoudn't be so hard but I need to find the max values of these. But somehow I cant do this.
Simple question, but today I can't do anything.

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 5 May 2020
Try using the brackets trick to extract the values out of the fields and concatenate into a vector:
allValues = [yourStructure.yourFieldName]
maxValue = max(allValues)
If your field is a row vector instead of a scalar, use vertcat():
allValues = vertcat(yourStructure.yourFieldName)
columnMaxValues = max(allValues, 1) % Max going down all the columns
rowMaxValues = max(allValues, 2) % Max going across all the rows.

  0 Comments

Sign in to comment.

More Answers (2)

Johannes Rebling
Johannes Rebling on 5 May 2020
Edited: Johannes Rebling on 5 May 2020
max([Struct(:).yourData])

  0 Comments

Sign in to comment.


Peter
Peter on 5 May 2020
Edited: Peter on 5 May 2020
If I'm understanding correctly, you have a struct array, s, of size 1x30, with a single field (let's call it x). Something like
s(1).x= 10
s(2).x = 11
...
s(30).x = 1.123
Use arrayfun to access the field of each struct in the array. Because there's a uniform output, you'll get a vector output. Then take the max:
max(arrayfun(@(z) z.x, s))
Alternatively, use the colon operator to access each element, turn it into a single array using the brackets, and then take the max:
max([s(:).x])
Hope this helps!

  1 Comment

Image Analyst
Image Analyst on 5 May 2020
Like I showed in my answer, the (:) is unneeded.

Sign in to comment.


Translated by