Info

This question is closed. Reopen it to edit or answer.

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

1 view (last 30 days)
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)

Star Strider
Star Strider on 19 Oct 2016
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
Star Strider
Star Strider on 19 Oct 2016
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.

Community Treasure Hunt

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

Start Hunting!