Assign new values to matrix using indices
- To: mathgroup at smc.vnet.net
- Subject: [mg99751] Assign new values to matrix using indices
- From: Mac <mwjdavidson at googlemail.com>
- Date: Wed, 13 May 2009 05:07:34 -0400 (EDT)
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