Matrix Multiplications (Altar Output Matrix Size)

2 次查看(过去 30 天)
I have a matrix of 100x10 multiplied with a matrix of 1x10. I need to get 100x10 values out as the result. Currently i only get 1x10 values as the ans. How could i modify the terms accordingly?
for i=1:1:100
for k=1:1:10
c1 = data(i,k) * p(1:k);
end
end
Also, are there any "computationally efficient" alternatives to a for loop in this case?
Please Advise. Thanks!
  2 个评论
Mischa Kim
Mischa Kim 2014-1-28
Hello Chockalingam, what exactly do you need to calculate? Do you want to multiply the 100x10 separately with each of the entries of the 1x10 to get 10 100x10's?
Chockalingam Kasi
Chockalingam Kasi 2014-1-30
Hi Mischa!
not exactly.. i want all elements in the (100x10) matrix to be multiplied with the (1x10) matrix.
Like each Row of the (100x10) to be multiplied with the (1x10) and finally i will get an output of (100x10).
Thanks!

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2014-1-28
c1 = bsxfun(@times,A,B);
  1 个评论
Chockalingam Kasi
Chockalingam Kasi 2014-1-30
Hi Andrei & mischa.. thanks!
For the bsxfun i get this error since my dimensions do not match-up "Non-singleton dimensions of the two input arrays must match each other."
My Matrix dimensions are "(100x10) x (1x10)".. In essence i want to multiply all 100 rows of the 1st matrix with the 2nd matrix individually. In the end i need a matrix of (100x10).
Cheers!

请先登录,再进行评论。

更多回答(3 个)

David Sanchez
David Sanchez 2014-1-28
A = rand(100,1); % place here your 100x10 matrix
B = rand(1,10); % place here your 1x10 matrix
C = A*B; % C is your 100x10 matrix

Mischa Kim
Mischa Kim 2014-1-30
OK, you mean something like:
A = [1 2; 3 4; 5 6];
b = [1 3];
C = zeros(size(A));
for ii = 1:length(A(:,1))
C(ii,:) = A(ii,:)'.*b';
end
where A,b,C correspond to your data,p,c matrices.

Jos (10584)
Jos (10584) 2014-1-30
编辑:Jos (10584) 2014-1-30
Your question is a little confusing. Do you intend to do element-by-element multiplication or matrix multiplication?
Assuming you're after element-by-element multiplication (c1(i,j) = data(i,j) x p(j), for i=1:100 and j=1:10) you can use bsxfun, as suggested by Andrei.
% small example
data = cumsum(repmat(1:3,4,1)) % a 4-by-3 matrix
p = [10 20 30] % a 1-by-3 vector
% engine
c1 = bsxfun(@times,data,p)
% check
i=2,j=3,isequal(c1(i,j), data(i,j) * p(j))
If this is not what you intended, clarify yourself ...

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by