Separating a structure field by unique elements?
显示 更早的评论
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!
回答(1 个)
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) ;
类别
在 帮助中心 和 File Exchange 中查找有关 Direct Search 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!