Borrar filtros
Borrar filtros

Matrix Multiplications (Altar Output Matrix Size)

3 visualizaciones (últimos 30 días)
Chockalingam Kasi
Chockalingam Kasi el 28 de En. de 2014
Comentada: Chockalingam Kasi el 31 de En. de 2014
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 comentarios
Mischa Kim
Mischa Kim el 28 de En. de 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 el 30 de En. de 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!

Iniciar sesión para comentar.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 28 de En. de 2014
c1 = bsxfun(@times,A,B);
  1 comentario
Chockalingam Kasi
Chockalingam Kasi el 30 de En. de 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!

Iniciar sesión para comentar.

Más respuestas (3)

David Sanchez
David Sanchez el 28 de En. de 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
  1 comentario
Mischa Kim
Mischa Kim el 28 de En. de 2014
Hello David, his first matrix is a 100x10. Yours is a 100x1.

Iniciar sesión para comentar.


Mischa Kim
Mischa Kim el 30 de En. de 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) el 30 de En. de 2014
Editada: Jos (10584) el 30 de En. de 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 ...

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by