フィルターのクリア

Matrix Multiplications (Altar Output Matrix Size)

3 ビュー (過去 30 日間)
Chockalingam Kasi
Chockalingam Kasi 2014 年 1 月 28 日
コメント済み: Chockalingam Kasi 2014 年 1 月 31 日
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
  1 件のコメント
Mischa Kim
Mischa Kim 2014 年 1 月 28 日
Hello David, his first matrix is a 100x10. Yours is a 100x1.

サインインしてコメントする。


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 ...
  1 件のコメント
Chockalingam Kasi
Chockalingam Kasi 2014 年 1 月 31 日
Hi Jos! Thanks much!
Cheers!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by