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: [mg117009] Re: Set diagonal of square matrix
  • From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
  • Date: Mon, 7 Mar 2011 05:49:23 -0500 (EST)
  • References: <201103051104.GAA29846@smc.vnet.net>

On Sat, 5 Mar 2011, Szabolcs 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.
>
>

here is one that works with SparseArray and Band

mat + SparseArray[Band[{1, 1}] -> vec - Diagonal[mat],
   Dimensions[mat]]

Oliver


  • Prev by Date: Show Rotate doesn't work
  • Next by Date: Re: Prepending Data to a File
  • Previous by thread: Set diagonal of square matrix
  • Next by thread: Re: Set diagonal of square matrix