f

#### diagonal matrix

```Hi,

I have a 200x200 matrix and I wish to write certain values
(a vector of 1x200) to its diagonal. Can you suggest a way
to do it without using loop? Is there any direct command
to write to the diagonal of a matrix?

Regards,
Sandeep
```
 0
12/6/2007 5:46:32 AM
comp.soft-sys.matlab 211266 articles. 21 followers. lunamoonmoon (257) is leader.

3 Replies
246 Views

Similar Articles

[PageSpeed] 0

```"Sandeep " <sandeep.gore@yahoo.com> wrote in message
<fj82bo\$a8b\$1@fred.mathworks.com>...
> Hi,
>
> I have a 200x200 matrix and I wish to write certain values
> (a vector of 1x200) to its diagonal. Can you suggest a way
> to do it without using loop? Is there any direct command
> to write to the diagonal of a matrix?
>
>

Use diag()function (it can do both: retrieve a diagonal and
to  build a diagonal matrix).

Or you can use linear indices bu sub2ind(). If A is (n x n)
matrix

ilindiag=sus2ind(size(A),1:n,1:n);
A(ilindiag) = mydiagonalvector;

Bruno
```
 0
brunoluong (348)
12/6/2007 7:08:50 AM
```"Sandeep " <sandeep.gore@yahoo.com> wrote in message
<fj82bo\$a8b\$1@fred.mathworks.com>...
> Hi,
>
> I have a 200x200 matrix and I wish to write certain values
> (a vector of 1x200) to its diagonal. Can you suggest a way
> to do it without using loop? Is there any direct command
> to write to the diagonal of a matrix?
>
>
> Regards,
> Sandeep

Yep, there is:

A = rand(5) ; % your original matrix
V = 1:5 ; % your values to be on the diagonal

A(eye(size(A))==1) = V ;

hth
Jos

```
 0
DELjos (627)
12/6/2007 8:20:35 AM
```Jos a �crit :

> A(eye(size(A))==1) = V ;

Or

A = rand(5) ; % your original matrix
V = 1:5;  % your values to be on the diagonal

A(1:size(A,1)+1:end)=V;

J�r�me
```
 0
dutmatlab (3797)
12/6/2007 8:46:29 AM