フィルターのクリア

How to multiply two matrices together?

2 ビュー (過去 30 日間)
Tristan
Tristan 2013 年 10 月 28 日
回答済み: Andrei Bobrov 2013 年 10 月 28 日
I need to create a new matrix C which is the sum of A multiplied by B(1)=1.7 and B(2)=1.1
>> A = [12 62 93 -8 22; 16 2 87 43 91; -4 17 -72 95 6]
A =
12 62 93 -8 22
16 2 87 43 91
-4 17 -72 95 6
>> B=[1.7 1.1]'
B =
1.7000
1.1000
I could just do:
>> A*1.7+A*1.1
ans =
33.6000 173.6000 260.4000 -22.4000 61.6000
44.8000 5.6000 243.6000 120.4000 254.8000
-11.2000 47.6000 -201.6000 266.0000 16.8000
but the real B I'm working with has too many numbers to just type them all
I tried bsxfun but it doesn't work
>> C=bsxfun(@times,A,B)
Error using bsxfun Non-singleton dimensions of the two input arrays must match each other.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 28 日
編集済み: Azzi Abdelmalek 2013 年 10 月 28 日
Notice that A*B(1)+A*B(2)+...+A*B(n) is equal to A*(B(1)+B(2)+...B(n))
A = [12 62 93 -8 22; 16 2 87 43 91; -4 17 -72 95 6];
B=[1.7 1.1]';
C=A*sum(B)
  2 件のコメント
Tristan
Tristan 2013 年 10 月 28 日
Thanks,
What if I had to add cosines or sines, as in C=cos(A*B)?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 28 日
A = [12 62 93 -8 22; 16 2 87 43 91; -4 17 -72 95 6];
B=[1.7 1.1]';
s=cell2mat(arrayfun(@(x) cos(A*x),B','un',0))
[n,m]=size(A);
p=numel(B);
C=sum(reshape(s,n,m,p),3)

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2013 年 10 月 28 日
out = sum(cos(bsxfun(@times,A,reshape(B,1,1,[]))),3)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by