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
2013년 11월 23일
G = X.'*Y;
댓글 수: 1
Harry MacDowel
2013년 11월 23일
This is so cool! Nice hack!
Azzi Abdelmalek
2013년 11월 23일
편집: Azzi Abdelmalek
2013년 11월 23일
n=4;
m=3;
X=rand(m,n); % Example
Y=rand(m,1);
G=sum(bsxfun(@times,X,Y))'
카테고리
도움말 센터 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!