Multipling a multidimensinnal array with a vector

조회 수: 2 (최근 30일)
John Hock
John Hock 2019년 4월 28일
댓글: Star Strider 2019년 5월 1일
Hi Everyone
I am trying to multiply a multidimensional 13*2560 (13 rows and 2560 coloumns) with 1*2560 (1 row and 2560 coloumns).
But matlab is giving error that matrix dimensions is nit match
For example as for simplest example
A=[1 2 3 4 ; 5 6 7 8]; 2*4 array
>> B=[11 22 33 44 ];1*4 vector
I want first row of A get multiplied with B and give its mean as a output then same for other row.
weighted_mean = mean(B.*A)
Please help
Thanks in advance

채택된 답변

Star Strider
Star Strider 2019년 4월 28일
편집: Star Strider 2019년 4월 28일
If you want the mean of each row, specify the dimension argument as 2:
weighted_mean = mean(B.*A, 2);
That should do what you want.
EDIT —
To calculate a mean of the rows with respect to the elements of the weighting vector (as described in Mathematical definition (link) of the Weighted aritmentic mean), this works:
WeightedMean = sum(bsxfun(@times, B, A),2) / sum(B);
equivalently (R2016b and later releases):
WeightedMean = sum(B.*A,2) / sum(B);
This of course assumes that ‘B’ is the weighting vector.
Experiment to get the result you want.
  댓글 수: 6
John Hock
John Hock 2019년 5월 1일
@Star Strider
Sorry to disturb you again but i again facing some problem
My matrix whose which I want to calculate weighted average z3(22*2562) and weighted vector
abc(1*2562)
Using the above mentioned command
WeightedMean = sum(bsxfun(@times, abc, z3),2) / sum(abc)
But I am expecting the output 1*2562 but it is giving output of just 22*1 values
Pictures are attached1111.PNG
Please help me in this regard
2222.PNG
Thanks in advance
3333.PNG
Star Strider
Star Strider 2019년 5월 1일
No worries.
If you want a (1 x 2562) result, sum down the columns rather than across the columns:
WeightedMean = sum(bsxfun(@times, abc, z3),1) / sum(abc);
or using the default column sum:
WeightedMean = sum(bsxfun(@times, abc, z3)) / sum(abc)
My only concern is that may not be the same thing as the true weighted mean, since you are applying the weighting vector across the columns, although summing down the columns.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by