Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.


Matt
Posts:
10
Registered:
9/17/12


Re: manual algorithm for matrix multiplication [3X2]*[2X3]
Posted:
Sep 29, 2012 2:57 PM


"Nasser M. Abbasi" <nma@12000.org> wrote in message <k459eh$vtm$1@speranza.aioe.org>... > On 9/28/2012 3:49 PM, Matt wrote: > > Having trouble coming up with a code that multiplies two matrices together. > >I can't just use matlab to directly multiply then, I have to come up with a loop. > > I am not sure how to implement a loop but doing something like this sum(X(1,:).*Y(:,1)') > >gets the first element in the multiplied matrix correct. > > > > Can you just implement it from the algorithm itself? C=A*B means C(i,j) is > adding the product of row i from A with column j from B ? something like: > >  > A=[3 4 1; > 2 3 4]; > > B=[ 1 2 3; > 4 5 6; > 7 8 9]; > > [nRow1,nCol1]=size(A); > [nRow2,nCol2]=size(B); > > if nCol1 ~= nRow2 > error('inner dimensions must match'); > end > > C = zeros(nRow1,nCol2); > > for i = 1:nRow1 > for j = 1:nCol2 > for k = 1:nCol1 > C(i,j) = C(i,j) + A(i,k)*B(k,j); > end > end > end > > C >  > > This gives > > C = > > 26 34 42 > 42 51 60 > > To verify > > A*B > > ans = > > 26 34 42 > 42 51 60 > > Nasser excellent.



