Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10597 > unrolled thread

Re: multiplying, element wise, a row by each column of a matrix.

Started byAlexei Boulbitch <Alexei.Boulbitch@iee.lu>
First post2012-06-22 09:14 +0000
Last post2012-07-02 09:30 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: multiplying, element wise, a row by each column of a matrix. Alexei Boulbitch <Alexei.Boulbitch@iee.lu> - 2012-06-22 09:14 +0000
    Re: multiplying, element wise, a row by each column of a matrix. 解红叶 <xiehongyester@gmail.com> - 2012-07-02 09:30 +0000

#10597 — Re: multiplying, element wise, a row by each column of a matrix.

FromAlexei Boulbitch <Alexei.Boulbitch@iee.lu>
Date2012-06-22 09:14 +0000
SubjectRe: multiplying, element wise, a row by each column of a matrix.
Message-ID<js1d1v$m8h$1@smc.vnet.net>
Any one can think of a better way to do this?

The problem: Given a row vector

    v={v1,v2}

and matrix with same number of columns as v, such as

mat={{a11, a12},
      {a21, a22},
      {a31, a32}};

I need to multiply v1 by the first column, then
multiply v2 by the second column. (i.e. scale each column
of the matrix by the corresponding "weight" from the
row).

This results in

  {{a11 v1, a12 v2},
    {a21 v1, a22 v2},
   {a31 v1, a32 v2}}

I wanted to find a 'smart' way or good command to do
it, but so far, had to do it the hard way. I'll show 2
methods. May be you can find a better functional way
to solve this:

1)
Transpose[MapThread[#1 #2 &, {v, Transpose[mat]}]]

This is not natural solution. need to transposes
2 times.

2)
KroneckerProduct[{v[[#]]},mat[[All,#]]]&/@ Range[1,Length[v]]
Transpose[Flatten[%, 1]]

also not too natural. Faltten, transpose, etc... not good.

Fyi, using another system, which starts with the letter
'O' and ends with the letters 'VE', I can do the above
using

       v .* mat

Where the ".*" above means element by element product.

I hope the experts here can find a short/better way to do
this. I am sure there is, I just can't find it yet, and
I am not happy with what I came up with so far.

thanks,
--Nasser

Hi, Nasser,

The scalar product (Dot[ ]) gives you almost the list like you need. Try this:

Clear[v1, v2];
mat = {{a11, a12}, {a21, a22}, {a31, a32}};
v = {v1, v2};

mat.v

{a11 v1 + a12 v2, a21 v1 + a22 v2, a31 v1 + a32 v2}

Now replace Plus by List:

mat.v /. Plus -> List

{{a11 v1, a12 v2}, {a21 v1, a22 v2}, {a31 v1, a32 v2}}

Have fun, Alexei


Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone :  +352-2454-2566
Office fax:       +352-2454-3566
mobile phone:  +49 151 52 40 66 44

e-mail: alexei.boulbitch@iee.lu



[toc] | [next] | [standalone]


#10743

From解红叶 <xiehongyester@gmail.com>
Date2012-07-02 09:30 +0000
Message-ID<jsrpob$d4j$1@smc.vnet.net>
In reply to#10597
v = {v1, v2}
mat = {{a11, a12}, {a21, a22}, {a31, a32}}
(*Why  not try this?,I think this is very good and simple. *)
mat.DiagonalMatrix[v]
In[135]:= mat.DiagonalMatrix[v]
Out[135]= {{a11 v1, a12 v2}, {a21 v1, a22 v2}, {a31 v1, a32 v2}}



=E5=9C=A8 2012=E5=B9=B46=E6=9C=8822=E6=97=A5=E6=98=9F=E6=9C=9F=E4=BA=94UTC+=
8=E4=B8=8B=E5=8D=885=E6=97=B614=E5=88=8639=E7=A7=92=EF=BC=8CAlexei Boulbitc=
h=E5=86=99=E9=81=93=EF=BC=9A
> Any one can think of a better way to do this?
>
> The problem: Given a row vector
>
>     v={v1,v2}
>
> and matrix with same number of columns as v, such as
>
> mat={{a11, a12},
>       {a21, a22},
>       {a31, a32}};
>
> I need to multiply v1 by the first column, then
> multiply v2 by the second column. (i.e. scale each column
> of the matrix by the corresponding "weight" from the
> row).
>
> This results in
>
>   {{a11 v1, a12 v2},
>     {a21 v1, a22 v2},
>    {a31 v1, a32 v2}}
>
> I wanted to find a 'smart' way or good command to do
> it, but so far, had to do it the hard way. I'll show 2
> methods. May be you can find a better functional way
> to solve this:
>
> 1)
> Transpose[MapThread[#1 #2 &, {v, Transpose[mat]}]]
>
> This is not natural solution. need to transposes
> 2 times.
>
> 2)
> KroneckerProduct[{v[[#]]},mat[[All,#]]]&/@ Range[1,Length[v]]
> Transpose[Flatten[%, 1]]
>
> also not too natural. Faltten, transpose, etc... not good.
>
> Fyi, using another system, which starts with the letter
> 'O' and ends with the letters 'VE', I can do the above
> using
>
>        v .* mat
>
> Where the ".*" above means element by element product.
>
> I hope the experts here can find a short/better way to do
> this. I am sure there is, I just can't find it yet, and
> I am not happy with what I came up with so far.
>
> thanks,
> --Nasser
>
> Hi, Nasser,
>
> The scalar product (Dot[ ]) gives you almost the list like you need. Try =
this:
>
> Clear[v1, v2];
> mat = {{a11, a12}, {a21, a22}, {a31, a32}};
> v = {v1, v2};
>
> mat.v
>
> {a11 v1 + a12 v2, a21 v1 + a22 v2, a31 v1 + a32 v2}
>
> Now replace Plus by List:
>
> mat.v /. Plus -> List
>
> {{a11 v1, a12 v2}, {a21 v1, a22 v2}, {a31 v1, a32 v2}}
>
> Have fun, Alexei
>
>
> Alexei BOULBITCH, Dr., habil.
> IEE S.A.
> ZAE Weiergewan,
> 11, rue Edmond Reuter,
> L-5326 Contern, LUXEMBOURG
>
> Office phone :  +352-2454-2566
> Office fax:       +352-2454-3566
> mobile phone:  +49 151 52 40 66 44
>
> e-mail: alexei.boulbitch@iee.lu

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web