Separating a structure field by unique elements?
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
KSSV
le 27 Fév 2019
Modifié(e) : KSSV
le 27 Fév 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 commentaires
Voir également
Catégories
En savoir plus sur Direct Search dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!