Re: Assign new values to matrix using indices
- To: mathgroup at smc.vnet.net
- Subject: [mg99771] Re: Assign new values to matrix using indices
- From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
- Date: Thu, 14 May 2009 01:40:00 -0400 (EDT)
- References: <gue2k2$7ll$1@smc.vnet.net>
Hi Mac, With m your matrix, (m - LowerTriangularize[m, -1]/2) does the trick. Cheers -- Sjoerd On May 13, 11:07 am, Mac <mwjdavid... at googlemail.com> wrote: > This simple problem has got me confounded (despite 2 years of > Mathematica experience). Say I would like to divide all lower diagonal > elements of a matrix by 2. It is fairly easy to generate a list of > indices, here an example for 3 by 3 matrix > > In[120]:= > Table[{j, i}, {j, 2, cdim = 3}, {i, 1, j - 1}] // Flatten[#, 1] & > > Out[120]= {{2, 1}, {3, 1}, {3, 2}} > > Now if I would like to divide the elements of a 3 x 3 matrix by 2, I > don't know how to do this. The problem is the complex interplay of Part > [], Set[] and Apply[]. To set the elements of hte matrix I can do > this > > In[219]:= t2 = {{0, 0, 0}, {2, 0, 0}, {2, 2, 0}}; > f = Function[{x}, Apply[Set[Part[t2, ##], 1] &, x]]; > Scan[f, {{2, 1}, {3, 1}, {3, 2}}] > t2 > > Out[222]= {{0, 0, 0}, {1, 0, 0}, {1, 1, 0}} > > which seems complicated for a simple operation. However, to divide all > elements with indices given in the form above i.e. {{2, 1}, {3, 1}, > {3, 2}} by 2 has got me stumped. > > Any help would be appreciated > > Mac