# Calculate weighted average of a 2D matrix

조회 수: 35 (최근 30일)
Abdul Wahab 2018년 8월 7일
댓글: stelios loizidis 2019년 5월 23일
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.

댓글을 달려면 로그인하십시오.

### 채택된 답변

James Tursa 2018년 8월 7일
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이전 댓글 3개 표시이전 댓글 3개 숨기기
James Tursa 2018년 8월 8일
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.
Abdul Wahab 2018년 8월 8일
This matrix is populated with a non-dimensional variable (376 rows) and associated time history of 481 hours. So each row is then a representation of how this non-dimensional variable is changing through 481 hours (one column for every hour). This non-dimensional number varies in a specific order where it contains both unique and non-unique values of this non-dimensional variable. I would like to figure out the weight vector of the non-unique and unique values in every row and get the weighted average instead of a simple mean. I hope this provides some more insight into my problem.

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Thorsten 2018년 8월 8일
X = rand(376, 481);
w = rand(1, size(X, 2));
Xw = bsxfun(@times, X, w);
m = mean(Xw, 2);
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
stelios loizidis 2019년 5월 23일
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?

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Configure Simulation Conditions에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by