### Tags: dot, matlab, matrices, product, programming, reshaped, versions, wherethe

# dot product of matrices

On Programmer » Matlab

2,953 words with 3 Comments; publish: Wed, 30 Apr 2008 18:26:00 GMT; (20062.50, « »)

Hello,

I have two matrices, say A = [1 2; 3 4] and B = [5 6; 7 8].

I need to take the dot product of the reshaped versions, where

the reshaped versions are vectors formed by stacking the rows

after one another, starting from the first row, then the second row

etc. The reshaped A would then be a = [1 2 3 4] and b= [5 6 7 8].

So I want to do a*b', i.e. [1 2 3 4]*[5;6;7;8]. Is it possible

to do this using A and B instead of using 'reshape' on A and B, i.e.

can one use 'dot' on A and B to get a*b' ? Or is there some other

faster way than 'dot' ?

-frege

*http://matlab.todaysummary.com/q_matlab_14134.html*

All Comments

Leave a comment...

- 3 Comments
- "frege" <frege.matlab.todaysummary.com.on_REMOVE_line.no> wrote in message
news:ef0269b.-1.matlab.todaysummary.com.webx.raydaftYaTP...

> Hello,

> I have two matrices, say A = [1 2; 3 4] and B = [5 6; 7 8].

> I need to take the dot product of the reshaped versions, where

> the reshaped versions are vectors formed by stacking the rows

> after one another, starting from the first row, then the second row

> etc. The reshaped A would then be a = [1 2 3 4] and b= [5 6 7 8].

> So I want to do a*b', i.e. [1 2 3 4]*[5;6;7;8]. Is it possible

> to do this using A and B instead of using 'reshape' on A and B, i.e.

> can one use 'dot' on A and B to get a*b' ? Or is there some other

> faster way than 'dot' ?

> -frege

dot(A(:)',B(:))

Brett

#1; Wed, 30 Apr 2008 18:28:00 GMT

- "frege" <frege.matlab.todaysummary.com.on_REMOVE_line.no> wrote in message
- Hi,
A = [1 2; 3 4]

B = [5 6; 7 8]

Then many solutions :

Use sum :

% 1)

sum(dot(A,B))

Use colon operator :

% 2)

dot(A(:),B(:))

You can also use ".*" instead of "*".

% 3)

M=A.*B;

sum(M(:))

% 4)

sum(sum(A.*B))

% 5)

sum(A(:).*B(:))

%...

Read these :

<http://www.mathworks.com/access/hel.../ref/colon.html>

<http://www.mathworks.com/access/hel...rs.htm

l>

help sum

<http://www.mathworks.com/access/hel...oc/ref/sum.html>

help dot

<http://www.mathworks.com/access/hel...oc/ref/dot.html>

Jrme

#2; Wed, 30 Apr 2008 18:29:00 GMT

- Hi,
- You can use trace. But it's more efficient to just evaluate it as
A(:)'*B(:). If the matrices are symmetric then there are additional tricks

you can do (such as scaling the diagonal elements and only taking products

using the upper triangles).

#3; Wed, 30 Apr 2008 18:30:00 GMT

- You can use trace. But it's more efficient to just evaluate it as