       Re: Re: Assign new values to matrix using indices

• To: mathgroup at smc.vnet.net
• Subject: [mg99777] Re: [mg99766] Re: Assign new values to matrix using indices
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Thu, 14 May 2009 01:41:09 -0400 (EDT)
• References: <gue2k2\$7ll\$1@smc.vnet.net> <200905131019.GAA11520@smc.vnet.net>

```I think Jens-Peer's method is the easiest, but for the sake of
"diversity" here is a  very different one:

m = {{a, b, c}, {d, e, f}, {h, i, j}};

Normal[SparseArray[{{i_, j_} /; i > j :> m[[i, j]]/2, {i_, j_} /; i <=
j :> m[[i, j]]}, Dimensions[m]]]

On 13 May 2009, at 19:19, Jens-Peer Kuska wrote:

> Hi,
>
> with
>
> m = {{a, b, c}, {d, e, f}, {h, i, j}};
>
> try
>
> MapIndexed[If[Greater @@ #2, #1/2, #1] &, m, {2}]
>
> Regards
>   Jens
>
> Mac 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:=
>> Table[{j, i}, {j, 2, cdim = 3}, {i, 1, j - 1}] // Flatten[#, 1] &
>>
>> Out= {{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:= 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= {{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
>>
>

```

• Prev by Date: Re: Label Bar Chart from data
• Next by Date: Re: Assign new values to matrix using indices
• Previous by thread: Re: Assign new values to matrix using indices
• Next by thread: Re: Assign new values to matrix using indices