Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a column vector to the diagonal of a matrix? [duplicate]

Tags:

r

excel

matrix

I have a column vector that I would like to convert to a diagonal matrix.
That is I have the vector v = (1,2,3) and want to turn it into:

[1, 0, 0]

[0, 2, 0]

[0, 0, 3]

How can I do this in Excel? in R?
I don't care how I do it, I just need to get it done. I know this is a simple question but I've been trying to figure this out for hours now.
(note: my vector is currently in a csv file)

like image 506
Dan Avatar asked Sep 05 '25 17:09

Dan


1 Answers

In R you simply have to do:

> v<- c(1, 2, 3)
> diag(v)
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    2    0
[3,]    0    0    3
like image 194
Yannis Vassiliadis Avatar answered Sep 08 '25 12:09

Yannis Vassiliadis