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: [mg116973] Re: Set diagonal of square matrix
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Sun, 6 Mar 2011 05:42:44 -0500 (EST)

On 3/5/11 at 6:04 AM, szhorvat at gmail.com (Szabolcs) wrote:

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

Use DiagonalMatrix. For example,

In[7]:= mat = RandomInteger[10, {3, 3}]

Out[7]= {{6, 4, 3}, {7, 10, 4}, {5, 3, 3}}

In[8]:= mat + DiagonalMatrix[Range[3] - Diagonal@mat]

Out[8]= {{1, 4, 3}, {7, 2, 4}, {5, 3, 3}}



  • Prev by Date: Re: Set diagonal of square matrix
  • Next by Date: Re: Plotting singularities
  • Previous by thread: Re: Set diagonal of square matrix
  • Next by thread: Re: Set diagonal of square matrix