Calculate weighted average of a 2D matrix
33 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Abdul Wahab
am 7 Aug. 2018
Kommentiert: stelios loizidis
am 23 Mai 2019
Dear all, I hope all is well. I am working with a 2D Matrix that is 376x481. I would like to calculate the weighted average of this matrix for each row, such that the desired output should be sized 376 X 1. I would greatly appreciate any help with this problem.
Thanks.
0 Kommentare
Akzeptierte Antwort
James Tursa
am 7 Aug. 2018
E.g.,
w = 1x481 vector of weights
M = your 376x481 matrix of values
result = sum(M.*w,2) / sum(w);
or
sum(bsxfun(@times,M,w),2) / sum(w);
5 Kommentare
James Tursa
am 8 Aug. 2018
Can you provide us with some info on what data this matrix contains and how it was generated? Then maybe we can make a stab at what you might want to do for weights.
Weitere Antworten (1)
Thorsten
am 8 Aug. 2018
X = rand(376, 481);
w = rand(1, size(X, 2));
Xw = bsxfun(@times, X, w);
m = mean(Xw, 2);
1 Kommentar
stelios loizidis
am 23 Mai 2019
Hello,
I saw the commands you wrote above and I have the following question; I have a matrix e.g
[ 5 3 3 1
3 4 5 2
5 0 0 0
3 4 5 2 ]
For this matrix I have to make a weighted average for each row. That is [5+3+(4/5)*(3+1)] / 4 =16/5. Also, if there is only one number and the rest zero (row 3) the weighted average should be [5+0+0+0]/ 1 =5 and the end we choose the largest weight average. How does this work?
Siehe auch
Kategorien
Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!