Path: news.mathworks.com!not-for-mail
From: "xiao chen" <yuanyuan5220985@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: how to vectorize this
Date: Tue, 25 Dec 2007 08:28:06 +0000 (UTC)
Organization: Nanjing university
Lines: 17
Message-ID: <fkqeum$j38$1@fred.mathworks.com>
Reply-To: "xiao chen" <yuanyuan5220985@gmail.com>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1198571286 19560 172.30.248.38 (25 Dec 2007 08:28:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 25 Dec 2007 08:28:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 497687
Xref: news.mathworks.com comp.soft-sys.matlab:443480



Hi
A very simple question:
I want to replace the diagonals of a matrix.
for example,
a=[1 1 1
   1 1 1
   1 1 1]
I need to change 1 in the diagonals to 2, and the result
a=[2 1 1
   1 2 1
   1 1 2]
I can write this code in a For loop.
I'm sure there is a much better way to do the same job, 
hmmm, writing it in a vectorized form. But I have not been 
able to figure one out. 

Can somebody help me, thanks!!!