find max value of structure

31 ビュー (過去 30 日間)
FV
FV 2020 年 5 月 5 日
コメント済み: Image Analyst 2023 年 9 月 27 日
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.

採用された回答

Image Analyst
Image Analyst 2020 年 5 月 5 日
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.

その他の回答 (2 件)

Johannes Rebling
Johannes Rebling 2020 年 5 月 5 日
編集済み: Johannes Rebling 2020 年 5 月 5 日
max([Struct(:).yourData])
  2 件のコメント
Tracy
Tracy 2023 年 9 月 27 日
Thank you! had thesame problem and surprisingly this works perfectly. cant beleive it was such a simple fix.
Image Analyst
Image Analyst 2023 年 9 月 27 日
@Tracy yes, and it could be simpler. Note in my Accepted Answer above I don't use (:) because it's unnecessary if the values are simply scalars. And you will want to assign the output of max() to some variable (like I did above). Otherwise it's just basically thrown away and you can't use it. Above I essentially did
maxValue = max([yourStructure.yourFieldName])
Or for his code...
Struct(1).yourData = 1;
Struct(2).yourData = 2;
maxValue = max([Struct.yourData]) % No (:) needed. And save the max value for use in subsequent code.

サインインしてコメントする。


Peter O
Peter O 2020 年 5 月 5 日
編集済み: Peter O 2020 年 5 月 5 日
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 件のコメント
Image Analyst
Image Analyst 2020 年 5 月 5 日
Like I showed in my answer, the (:) is unneeded.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by