Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Matrix Mult. Question

8 views
Skip to first unread message

Demetrios Tsaparas

unread,
Nov 28, 2005, 11:51:32 AM11/28/05
to
I have a vector:

P = [1 2 3]

And a matrix

Q = [ 1 2 3
3 4 5
6 7 8]

I want to multiply each element of Q by the corresponding column
element of P

For example, from above I would get

Q1 = [1 2 3
6 8 10
18 21 24]

The only catch is I was hoping not to use a loop or create a larger
matrix using repmat(p,1,3)

Thanks for your help,
D

mut ante

unread,
Nov 28, 2005, 12:09:38 PM11/28/05
to
Q.* P(ones(3,1),:)'

Demetrios Tsaparas

unread,
Nov 28, 2005, 12:26:07 PM11/28/05
to
mut ante wrote:
>
>
> Q.* P(ones(3,1),:)'
Thank you very much.
Is there anyway of doing this without creating increasing the size of
P?

Thanks again for your help
D

mut ante

unread,
Nov 28, 2005, 1:02:38 PM11/28/05
to
hi:

i think that you wont be able to do it with a matrix multiplication
because if the first matrix has size [nxm] and the second [oxp] the
result will be [nxp].... and for matrix multiplication to exist you
also need m=o.... so if you want the result matrix to have [3x3] you
will need n = 3 and p=3.... the fact m = o will force you to
increase p length...

using element by element matrix multiplication you will also need to
make both sizes of multiplying matrices equal to the size of the
result matrix....

if anyone has a more creative way of doing what is proposed i would
be glad to know about it....

see you

Randy Poe

unread,
Nov 28, 2005, 1:02:22 PM11/28/05
to

Demetrios Tsaparas wrote:
> mut ante wrote:
> >
> >
> > Q.* P(ones(3,1),:)'
> Thank you very much.
> Is there anyway of doing this without creating increasing the size of
> P?

This isn't increasing the size of P. P is unchanged after
this operation.

- Randy

Demetri T

unread,
Nov 28, 2005, 1:38:06 PM11/28/05
to
You are correct it doesn't change the final size of P.
However, I was wondering how P is 'manipulated' in order to perform
the matrix mult. The program I am trying to write will have very
large matrix dimensions, as a result, if p is temporarily enlarged
from [1xM] to [NxM]’ the program might slow down a great deal.
Specifically, I was wondering how efficient this would be. I am
trying to find the fastest way to perform this operation.

Thanks for your help,
Demetri

Randy Poe

unread,
Nov 28, 2005, 1:58:31 PM11/28/05
to

Demetri T wrote:
> You are correct it doesn't change the final size of P.
> However, I was wondering how P is 'manipulated' in order to perform
> the matrix mult. The program I am trying to write will have very
> large matrix dimensions, as a result, if p is temporarily enlarged
> from [1xM] to [NxM]' the program might slow down a great deal.

You're correct that a temporary large matrix is created. If N and
M are very large, you could run into a memory-full error and
the command wouldn't execute. In that case you'd need another
approach, like doing it a column at a time. But if memory is
sufficient to create the temporary matrix, the operation should
be fast. Matlab is pretty well optimized.

> Specifically, I was wondering how efficient this would be. I am
> trying to find the fastest way to perform this operation.

If there is sufficient PHYSICAL memory, using built-in matrix
operations
is fastest. If virtual memory and disk swaps are required, that will
slow things down.

- Randy

StephenLL

unread,
Nov 28, 2005, 2:17:10 PM11/28/05
to

Having gone through this before and not knowing how big the matrices
are going to be: I would suggest writing a mex function. I usually
have to element wise muliply each row of a matrix by a row vector or
each column by a column vector.

It was happening enough and the size of the matrices varied from very
small to very large (where the usually repmat, etc tricks took too
much memory) that I was forced to write a generic mex function to
handle these situations.

These are the times where I wish Matlab worked similar to R
(www.r-project.org). R knows to automatically "reuse" the row vector
each time.

If I had to pick two features to bring from R to Matlab it would be
the data "reuse" feature and how R allows for arguements such as
myfunc(data = adefault, paramb = default2) so when you call the
function you can either: pass variables in any order and two omit
variables since the defaults are right their in the declaration.

Stephen

us

unread,
Nov 28, 2005, 2:37:43 PM11/28/05
to
Demetri T:
<SNIP gen mat-vec op evergreen...

1) have a look at <doug schwarz>'s toolbox available here
<http://www.frontiernet.net/~dmschwarz/genops.html>

2) another one of the many solutions

function a=foo(a,op,b)
% note: input check is removed for sake of brevity
sop='+-*/';
mop={@plus,@minus,@times,@rdivide};
op=mop{sop==op};
for i=1:size(a,2)
a(:,i)=op(a(:,i),b);
end

us

John D'Errico

unread,
Nov 28, 2005, 6:59:13 PM11/28/05
to
In article <ef1cd...@webx.raydaftYaTP>,
"Demetrios Tsaparas" <spartan...@hotmail.com> wrote:

The best way to do this without a loop and without
some variant of repmat or a mex function is with a
sparse diagonal matrix multiply.

P = [1 2 3]

Q = [ 1 2 3
3 4 5
6 7 8]

n = length(P);
Q1 = spdiags(P',0,n,n)*Q

Q1 =
1 2 3
6 8 10
18 21 24

This should be efficient both in time and memory,
although it would probably not be as efficient as a
mex function. It should be quite reasonable though.

HTH,
John D'Errico

--
The best material model of a cat is another, or preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945

Those who can't laugh at themselves leave the job to others.
Anonymous

0 new messages