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


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

Re: An easier functional way to divide each Column of matrix by a row vector, element-wise?

Started byRay Koopman <koopman@sfu.ca>
First post2012-06-30 09:18 +0000
Last post2012-07-01 06:04 +0000
Articles 2 — 2 participants

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


Contents

  Re: An easier functional way to divide each Column of matrix by a row vector, element-wise? Ray Koopman <koopman@sfu.ca> - 2012-06-30 09:18 +0000
    Re: An easier functional way to divide each Column of matrix by a "Nasser M. Abbasi" <nma@12000.org> - 2012-07-01 06:04 +0000

#10697 — Re: An easier functional way to divide each Column of matrix by a row vector, element-wise?

FromRay Koopman <koopman@sfu.ca>
Date2012-06-30 09:18 +0000
SubjectRe: An easier functional way to divide each Column of matrix by a row vector, element-wise?
Message-ID<jsmg8v$1k2$1@smc.vnet.net>
On Jun 28, 1: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} }

What's "unnatural" is that there is no convenient way to operate on
the columns of a matrix, as opposed to the rows. Tranposing to make
the columns rows, operating on the rows, and then re-transposing may
seem awkward, but it's fast, especially if mat has many rows. If you
want code that's both fast and readable, consider defining

T = Transpose,

perhaps even in your init.m. Then you can write things like

T[T@mat/v].

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

[toc] | [next] | [standalone]


#10709 — Re: An easier functional way to divide each Column of matrix by a

From"Nasser M. Abbasi" <nma@12000.org>
Date2012-07-01 06:04 +0000
SubjectRe: An easier functional way to divide each Column of matrix by a
Message-ID<jsopa0$os$1@smc.vnet.net>
In reply to#10697
On 6/30/2012 4:18 AM, Ray Koopman wrote:

> What's "unnatural" is that there is no convenient way
>  to operate on the columns of a matrix, as opposed to the rows.

Yes. Also some functions support an argument which
can be used to tell it to do things 'row-wise' vs.
'column-wise'

(sorry, I am using Matrix terms here, but for me, this is
more natural).

For example, if A,B are matrices, and we wanted to join them
side-by-side (i.e. column wise):

Join[A,B,2]

To join them on top of each others (i.e. stack them, row-wise),

Join[A,B,1]

Another example. Given Matrix A and to find the Total
of all the rows (i.e row-wise), then the command is

Total[A,{2}]

and to find the total of all the columns

Total[A,{1}]

So, some commands do not require Transpose as they have
an option to tell it 'which way' to do things.

> Tranposing to make
> the columns rows, operating on the rows, and then re-transposing may
> seem awkward, but it's fast, especially if mat has many rows.

I think Mathematica is definitely biased towards rows. I think
the original designers of Mathematica liked rows much more
than columns :)

--Nasser

[toc] | [prev] | [standalone]


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


csiph-web