Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to vectorize this
Date: Sat, 29 Dec 2007 19:09:29 +0000 (UTC)
Organization: Universit&#228;t Stuttgart
Lines: 13
Message-ID: <fl6619$3cp$1@fred.mathworks.com>
References: <fkqeum$j38$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1198955369 3481 172.30.248.37 (29 Dec 2007 19:09:29 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 29 Dec 2007 19:09:29 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 545430
Xref: news.mathworks.com comp.soft-sys.matlab:443818



If you have a square matrix with N rows and columns, you can
index the diagonal elements like this:

N=3;
A=ones(N);
A(1:(N+1):end)=2
A =
     2     1     1
     1     2     1
     1     1     2

Regards
Markus