Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: manual algorithm for matrix multiplication [3X2]*[2X3]
Replies: 3   Last Post: Sep 29, 2012 3:54 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Matt

Posts: 10
Registered: 9/17/12
Re: manual algorithm for matrix multiplication [3X2]*[2X3]
Posted: Sep 29, 2012 2:57 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"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.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.