Another approach passing both D and delta: > Afct <- function(D, delta) exp(-(outer(D, D, "-")/delta)^2) > D <- c(-1, -2/3, -1/3, 0, 1/3, 2/3, 1) > Delta <- 2 > Afct(D, delta)
------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Blaser Nello Sent: Wednesday, June 26, 2013 11:03 AM To: Jennifer Tickner; r-help@r-project.org Subject: Re: [R] Creating a matrix with an unknown variable You have to define a function. For instance: Afct <- function(delta){ D <- c(-1, -2/3, -1/3, 0, 1/3, 2/3, 1) Dmat <- matrix(D, nrow=7, ncol=7) Smat <- Dmat-t(Dmat) A <- exp(-(Smat/delta)^2) return(A) } Afct(2) Also try to avoid loops... Best, Nello -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Jennifer Tickner Sent: Mittwoch, 26. Juni 2013 10:18 To: r-help@r-project.org Subject: [R] Creating a matrix with an unknown variable Hi all I'm trying to create a matrix, A, with an unknown variable delta. The code I have so far is: D<-c(-1, -2/3, -1/3, 0, 1/3, 2/3, 1) A<-matrix(NA,nrow=7,ncol=7) for (i in 1:7) { for (j in 1:7) { A[i,j]<-exp(-((D[i]-D[j])/delta)^2) } } Of course, R comes up with an error message because delta is not yet defined, but works if I plug in a value for delta. However, I want R to use the term delta in the matrix A, because I'm trying to estimate delta in another equation that is written in terms of A. Can anyone help? Many thanks Jen [[alternative HTML version deleted]] ______________________________________________ R-help@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. ______________________________________________ R-help@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. ______________________________________________ R-help@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.