# Re: [R] Extracting diagonal matrix

From: Anup Menon <anup.nandialath_at_gmail.com>
Date: Fri, 21 Nov 2008 10:46:54 -0500

Hi Ezhil,

Maybe this will help. There might be an easier way to do this but here is one solution.

tril <- function(A)
{
A <- as.matrix(A)
cmats <- matrix(rep(1,length(A)),dim(A)[1],dim(A)[2])  upper.tri(cmats,diag=T)
cmats[upper.tri(cmats)] <- 0
out <- A*cmats
return(out)
}

tril(correlation.matrix)

will give you the output.

HTH Best Regards

Anup

> Dear All,
>
> I have a correlation matrix of size 100 x 100 and would like to extract the
> diagonal matrix from it. I have used the for loop to store tha correlation
> values of the diagonal matrix. Is there a 'R way' of doing this?
>
>
> Kind regards,
> Ezhil
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 21 Nov 2008 - 15:57:50 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 21 Nov 2008 - 17:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.