Average column data with specific values
4 次查看(过去 30 天)
显示 更早的评论
I have the data below, I am trying to average the column gdppc for each country.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1105445/image.png)
0 个评论
采纳的回答
Robert U
2022-8-23
Hi Faustine Enos,
I do not know how your data is available and what data type you chose. It is preferrable to use table type data. Then, you can adjust the following code snippet:
col_1 = {'Country A';'Country A';'Country A';'Country A';'Country B';'Country B';'Country B';'Country C'};
col_2 = [1; 2; 3; 4; 5; 6; 7; 8];
data = table(col_1,col_2,'VariableNames',{'Country','value'});
cCountry = unique(data.Country);
for ik = 1:numel(cCountry)
sumVal(ik) = sum(data.value(ismember(data.Country,cCountry{ik})));
disp(sprintf('%s %d',cCountry{ik},sumVal(ik)));
end
Kind regards,
Robert
更多回答(1 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!