MATLAB Answers

Separating a structure field by unique elements?

8 views (last 30 days)
Olivia Colombo
Olivia Colombo on 27 Feb 2019
Edited: KSSV on 27 Feb 2019
In part C of this code, I want to determine which country in each continent has the largest agricultural water withdrawl, and then display the names of the continents and country.
I know what I have is wrong, but I'm stuck on how to split a field by the unique continents, find the max of another field and then output the country related field. I know I probably need to make individual tables with data from each continent but I can't figure out how to do that either. Please help!

  0 Comments

Sign in to comment.

Answers (1)

KSSV
KSSV on 27 Feb 2019
Edited: KSSV on 27 Feb 2019
You need not to convert the Table into structure.......Table is very elegant and you can do what ever you want. Check the below code:
T1 = readtable('aquastat.csv') ;
T = T1(1:200,[2 3 5 9 13]);
T.Properties.VariableNames = {'Country' 'Continent' 'Agricultural' 'Industrial' 'Municipal'};
[continent,ia,ib] = unique(T.Continent) ; % you can use T.(2) also
N = length(continent) ;
country = cell(N,1) ;
val = zeros(N,1) ;
for i = 1:N
idx = ib==i ;
[val0,idx0] = max(T.Agricultural(idx)) ;
val(i) = val0 ;
C = T.Country(idx) ;
country(i) = C(idx0) ;
end
iwant = table(continent,country,val) ;

  0 Comments

Sign in to comment.


Translated by