Maximum and Minimum values of a field structure array

Hi, I have structure array as shown in the image below. How can i find the maximum and minimum population with the corresponding state for the available data.
Thanks in advance.

回答 (1 件)

Birdman
Birdman 2020 年 4 月 2 日

0 投票

%find max and min population
[Cmax,maxIdx]=max(AustralianStates.Population)
[Cmin,minIdx]=min(AustralianStates.Population)
%find corresponding state
AustralianStates.Name([minIdx maxIdx])
%find corresponding land area
AustralianStates.Land_Area([minIdx maxIdx])

8 件のコメント

Rishitha Muchinthala
Rishitha Muchinthala 2020 年 4 月 3 日
It is showing as Error using max, too many input arguments. can you help me.
Birdman
Birdman 2020 年 4 月 3 日
Which version of MATLAB are you using? Can you also share your struct in a mat file?
Akira Agata
Akira Agata 2020 年 4 月 3 日
I believe small modification is needed:
%find max population
[Cmax,maxIdx]=max([AustralianStates.Population]);
%find corresponding state
AustralianStates.Name(maxIdx)
%find corresponding land area
AustralianStates.Land_Area(maxIdx)
FYI, another possible solution to find max population is:
%find max population
T = struct2table(AustralianStates);
[Cmax,maxIdx]=max(T.Population);
Rishitha Muchinthala
Rishitha Muchinthala 2020 年 4 月 3 日
Iam using R2019a.
Birdman
Birdman 2020 年 4 月 3 日
Can you try Akira's solution?
Rishitha Muchinthala
Rishitha Muchinthala 2020 年 4 月 3 日
Please find the matlab file attached below.
Rishitha Muchinthala
Rishitha Muchinthala 2020 年 4 月 3 日
Hi @Akira Agata,
I tried the first code and i got the below error.
"Expected one output from a curly brace or dot
indexing expression, but there were 8 results.
Error in A2_19508724 (line 108)
AustralianStates.Name(maxIdx)"
Rishitha Muchinthala
Rishitha Muchinthala 2020 年 4 月 3 日
Thank you,
Akira's second code is working.
I got the result.

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

カテゴリ

製品

リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by