Search All of the Math Forum:

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

Topic: Re: An easier functional way to divide each Column of matrix by a row
Replies: 0

 Dana DeLouis Posts: 107 Registered: 12/14/08
Re: An easier functional way to divide each Column of matrix by a row
Posted: Jul 2, 2012 10:17 PM

> mat = {{a1,a2},{b1,b2}};
> v = {v1,v2};
> Inner[Divide,mat,v,List]

Hi. The ideas given by others in the similar thread (about multiplying) could apply here.

mat={{a1,a2},{b1,b2}};
v={v1,v2};

// Yours
Inner[Divide,mat,v,List]
{{a1/v1,a2/v2},{b1/v1,b2/v2}}

Map[#/v &,mat]
{{a1/v1,a2/v2},{b1/v1,b2/v2}}

%% == %
True

mat = {{1,2},{3,4}};
v={5,10};

Inner[Divide,mat,v,List]
{{1/5,1/5},{3/5,2/5}}

Map[#/v &,mat]
{{1/5,1/5},{3/5,2/5}}

%% == %
True

If one inverts the vector at the start, one has the same solution as the other tread using Times.

mat={{a1,a2},{b1,b2}};
v=1 / {v1,v2};

Map[# * v &, mat]
{{a1/v1,a2/v2},{b1/v1,b2/v2}}

= = = = = = = = = =
HTH :>)
Dana DeLouis
Mac & Math 8
= = = = = = = = = =

On Jun 28, 4:03 am, "Nasser M. Abbasi" <n...@12000.org> wrote:
> hello;
>
> I found a better solution.
>
> (after a strong coffee and staring on it for sometime)
>
> method(5)
> -----------
> mat = {{a1,a2},{b1,b2}};
> v = {v1,v2};
> Inner[Divide,mat,v,List]
>
> Out[61]= { {a1/v1, a2/v2}, {b1/v1,b2/v2} }
>
> But I can't say though it was easy and intuitive to find
> for me but at least the above solution is a functional and
> I think the right Mathematica way of doing it. So I am
> happy. Was good practice though.
>
> --Nasser
>
> On 6/27/2012 8:13 PM, Nasser M. Abbasi wrote:
>
>
>

> > I have a list like this 'mat' and 'v' like this
>
> > mat = { {a1,a2},{b1,b2} }
> > v = {v1,v2}

>
> > I want to generate
>
> > mat={ {a1/v1, a2/v2}, { b1/v1, b2/v2 } }
>
> > I can't just do mat/v since this does
>
> > mat={ {a1/v1, a2/v1}, { b1/v2, b2/v2 } }
>
> > I solved this 2 ways, but I am still not happy.
> > I think there should be an easier way.

>
> > method 1 (not too natural)
> > -------------------------------
> > Clear["Global`*"]
> > mat={{a1,a2},{b1,b2}};
> > v={v1,v2};
> > Transpose[Transpose[mat]/v]

>
> > Out[93]= { {a1/v1, a2/v2}, {b1/v1, b2/v2} }
>
> > method 2 (too complicated)
> > ---------------------------
> > In[94]:= MapIndexed[Divide[#1,v[[#2[[2]]]]]&,mat,{2}]

>
> > Out[94]= { {a1/v1, a2/v2}, {b1/v1, b2/v2}}
>
> > method 3 (using a Table, oh no !)
> > ------------------------------------
> > In[96]:= Table[mat[[i,j]]/v[[j]],{i,2},{j,2}]

>
> > Out[96]= {{a1/v1, a2/v2} , {b1/v1, b2/v2} }
>
> > method 4 (a not good way to do it )
> > ----------------------------------------
> > In[108]:= mat.v/.Plus->List/.Times->Divide

>
> > Out[108]= {{a1/v1, a2/v2}, {b1/v1, b2/v2}}
>
> > I looked at real Mathematical tricks using Inenr and Outer and
> > something like this, but I do see a way so far. (I also did not
> > have my morning coffee yet), so I wanted to ask if
> > someone can see one of those elegant super functional
> > correct ways to do this.

>
> > ps. fyi, in that other system (starts with O and ends with VE)
> > I can do this like this:

>
> > mat=[1 2;3 4]
> > v=[5 10]
> > bsxfun(@rdivide,mat,v)

>
> > 0.20000 0.20000
> > 0.60000 0.40000

>
> > thanks,
> > --Nasser