sum of product of 2 matrices
6 次查看(过去 30 天)
显示 更早的评论
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 ?
0 个评论
回答(2 个)
Zoltán Csáti
2015-1-10
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 个评论
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
2015-1-10
sum(bsxfun(@times, u(c, :)', r))
3 个评论
Zoltán Csáti
2015-1-10
Guillaume provided a vectorized version, that's why there is no matrix index j.
Guillaume
2015-1-10
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!