hi
i have 2 matrices... u is a 2 x 1250 matrix and r is a 1250 x 1250 matrix...
here c = 1
for each j = 1 to 1250 i want to find
how to writ the matlab code for this ?

답변 (2개)

Zoltán Csáti
Zoltán Csáti 2015년 1월 10일

2 개 추천

It is just a standard matrix-matrix product: u*r. So enter
value = u(1,:)*r;
The j-th column of the matrix "value" will contain the sum for the specific j.

댓글 수: 4

Sreejith
Sreejith 2015년 1월 10일
This is only the product..
i want to find the sum of the products
u(c,1)*r(1,j) + u(c,2)*r(2,j) +.....+u(c,i)*r(i,j) where i = 1,2,3...,1250
what will be the code ?
Matt J
Matt J 2015년 1월 10일
But u(1,:)*r is a matrix product. It is equivalent to the summation you've shown.
Zoltán Csáti
Zoltán Csáti 2015년 1월 10일
This is what my code does. Write a few indices and you will see.
John D'Errico
John D'Errico 2015년 1월 10일
편집: John D'Errico 2015년 1월 10일
+1. What you don't seem to understand is that the * operator IS a dot product, i.e., the sum of products of elements. After all, MATLAB is a matrix language, so it is written to do these computations trivially, with the basic operators.
You sum of products is simply a dot product, what * delivers.

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

Guillaume
Guillaume 2015년 1월 10일

1 개 추천

Use bsxfun:
sum(bsxfun(@times, u(c, :)', r))

댓글 수: 3

Sreejith
Sreejith 2015년 1월 10일
In your answer, there is no mentioning of 'j'...
sum(bsxfun(@times, u(c,:)', r(:,j))
Is this correct ?
Zoltán Csáti
Zoltán Csáti 2015년 1월 10일
Guillaume provided a vectorized version, that's why there is no matrix index j.
bsxfun replicates u(c,:)' for each column j of r and sum calculates the sum for each of these columns. Exactly what you asked for.
Just try my code without any modification.
Note, this is equivalent to:
tempu = repmat(u(c, :)', 1, size(r, 2));
p = tempu .* r;
result = sum(p)
Or, if you really want to introduce j and make the calculation much slower:
tempu = repmat(u(c, :)', 1, size(r, 2));
p = tempu .* r;
result = zeros(1, size(r, 2));
for j = 1:size(r, 2)
result(j) = sum(p(:, j));
end
The one line bsxfun does the same.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2015년 1월 10일

편집:

2015년 1월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by