matrix and vector multiplication without for loop

Hi, I have 3 matrices "X" is m x n matrix and "Y" is m x 1 column vector, I want to multiply each column of X by the vector Y and calculate the sum of each multiplied column and put them in column vector "G" which is n x 1. Is there any way to do this without using for loops (vectorized solution) ?

回答 (2 件)

Roger Stafford
Roger Stafford 2013 年 11 月 23 日

3 投票

G = X.'*Y;

1 件のコメント

Harry MacDowel
Harry MacDowel 2013 年 11 月 23 日
This is so cool! Nice hack!

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

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 23 日
編集済み: Azzi Abdelmalek 2013 年 11 月 23 日

0 投票

n=4;
m=3;
X=rand(m,n); % Example
Y=rand(m,1);
G=sum(bsxfun(@times,X,Y))'

カテゴリ

ヘルプ センター および File ExchangeLinear Algebra についてさらに検索

質問済み:

2013 年 11 月 23 日

コメント済み:

2013 年 11 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by