Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to vectorize this
Date: Sun, 30 Dec 2007 05:41:13 +0000 (UTC)
Organization: The MathWorks Inc
Lines: 31
Message-ID: <fl7b1p$7c0$1@fred.mathworks.com>
References: <fkqeum$j38$1@fred.mathworks.com> <fl6619$3cp$1@fred.mathworks.com>
Reply-To: <HIDDEN>
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 1198993273 7552 172.30.248.38 (30 Dec 2007 05:41:13 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 30 Dec 2007 05:41:13 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1061251
Xref: news.mathworks.com comp.soft-sys.matlab:443832




Hi Xiao,

Another possible approach:

    N = 3;
    A = ones(N);
    idx = logical(eye(N));
    A(idx) = 2;

I hope that helps.  Thank you very much.

Rick Rosson


"Markus Buehren" <mb_matlab.REMOVE@gmxTHIS.de> wrote in message 
<fl6619$3cp$1@fred.mathworks.com>...
> 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