MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Set diagonal of square matrix

  • To: mathgroup at smc.vnet.net
  • Subject: [mg117076] Re: Set diagonal of square matrix
  • From: Ray Koopman <koopman at sfu.ca>
  • Date: Wed, 9 Mar 2011 06:58:56 -0500 (EST)
  • References: <ikt5e6$t68$1@smc.vnet.net> <ikvohg$f4u$1@smc.vnet.net>

On Mar 6, 2:44 am, Ray Koopman <koop... at sfu.ca> wrote:
> On Mar 5, 3:05 am, Szabolcs <szhor... at gmail.com> wrote:
>
>> Dear MathGroup members,
>>
>> Is there any simple way to set the diagonal of a square matrix to a
>> vector (of appropriate length)?  Of course there are ways to do it,
>> but I was wondering if there was a simple, one-line way.
>
> ReplacePart[{{0,0,0}, {0,0,0}, {0,0,0}}, {i_,i_} :> {a,b,c}[[i]] ]
>
> {{a,0,0}, {0,b,0}, {0,0,c}}

A pre- version 6 equivalent of

  ReplacePart[matrix, {i_,i_} :> vector[[i]] ]

would be

  With[{r = Range@Length@matrix},
       ReplacePart[matrix, vector, {#,#}&/@r, List/@r ]]


  • Prev by Date: NDSolve is using Solve?
  • Next by Date: Re: Weird Position[list,form] behavior when used inside of Module.
  • Previous by thread: Re: Set diagonal of square matrix
  • Next by thread: Plotting singularities