Matrix Multiplications (Altar Output Matrix Size)

1 view (last 30 days)
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 Comments
Mischa Kim
Mischa Kim on 28 Jan 2014
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 on 30 Jan 2014
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!

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 28 Jan 2014
c1 = bsxfun(@times,A,B);
  1 Comment
Chockalingam Kasi
Chockalingam Kasi on 30 Jan 2014
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!

Sign in to comment.

More Answers (3)

David Sanchez
David Sanchez on 28 Jan 2014
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 on 30 Jan 2014
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) on 30 Jan 2014
Edited: Jos (10584) on 30 Jan 2014
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 ...

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!