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


Groups > comp.soft-sys.math.mathematica > #10582

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

From Bob Hanlon <hanlonr357@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: multiplying, element wise, a row by each column of a matrix.
Date 2012-06-22 06:59 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <js155f$kcu$1@smc.vnet.net> (permalink)
References <201206210919.FAA16006@smc.vnet.net>

Show all headers | View raw


v#&/@mat

Which is abbreviation for

Map[v*#&, mat]

or

Map[Function[x, v*x], mat]


Bob Hanlon


On Jun 21, 2012, at 5:19 AM, "Nasser M. Abbasi" <nma@12000.org> wrote:

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

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: multiplying, element wise, a row by each column of a matrix. Bob Hanlon <hanlonr357@gmail.com> - 2012-06-22 06:59 +0000

csiph-web