How to calculate the weighted average
Show older comments
I have 3 companies. Each one of them has a ME, a Price and a Return (3 rows) with 6 elements each (6 columns). I want to find the Weighted Average of the 3 companies for each year (column). Multiple each element of the 1st Row with each element of the 3rd Row and then divide by the sum of weights (in each column). Then I need to repeat the same task for 10 different sheets.
Thank you
2 Comments
Benjamin Thompson
on 4 Oct 2022
If you are wanting to do this in MATLAB rather than a spreadsheet application like Excel, then attach some of the sample input data.
ALEXANDRA
on 5 Oct 2022
Accepted Answer
More Answers (1)
Image Analyst
on 4 Oct 2022
Edited: Image Analyst
on 4 Oct 2022
Use readmatrix and tell it what sheet to read in. Then take your data and weights and do an element by element mutliplication and sum:
data = readmatrix(filename, 'sheet', 'whatever');
% Multiple each element of the 1st Row with each element of the 3rd Row
v = data(1, :) .* data(3, :)
% and then divide by the sum of weights (in each column)
weights = I have no idea where to get these. How are you getting the weights????
weightedMean = v ./ sum(weights)
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Categories
Find more on Data Import and Export in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!