Is there a function to sum up the WeightedAVG for each same country?

For example, we would find a new table with only one row 'France' with WeightedABG_ = 0.006921 + 0.0045263.
Etc etc
Many thanks in advance !
Pierre

Answers (1)

There is no single function, but it is straightforward to do that calculation. See sum the TWh/year (the last column) when the name of the countries (the first column) are similar that solves essentially the same problem.

2 Comments

Thank you for your answer ! How can i replace a value in a table ?
My pleasure.
The easiest way is to extract the data from the table with the table2cell function, then do the calculations. You can re-create the table with the new values after that, as I did in my code.
That will create a cell array for the ‘WeightedAverage’ column, so you will have to change my code slightly to be compatible with that.
Example:
WgtAvgCell = {randi(5,10,1)}; % Create Data
ic = randi(10, 10, 1); % Create Data
WgtAvgSum = accumarray(ic, WgtAvgCell{:}); % Calculate Sums By Country
Note the use of curly braces ‘{}’ to create a numeric array from the cell array. If the cell returned by table2cell is different, and the simpler syntax here does not work, use the cell2mat function to produce a numeric array from it.

This question is closed.

Asked:

on 19 Oct 2016

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!