Azzera filtri
Azzera filtri

Matrix Multiplications (Altar Output Matrix Size)

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Mischa Kim
Mischa Kim il 28 Gen 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 il 30 Gen 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!

Accedi per commentare.

Risposta accettata

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

Accedi per commentare.

Più risposte (3)

David Sanchez
David Sanchez il 28 Gen 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 il 30 Gen 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) il 30 Gen 2014
Modificato: Jos (10584) il 30 Gen 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 ...

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by