[R] Creating a matrix with an unknown variable

Blaser Nello nblaser at ispm.unibe.ch
Wed Jun 26 18:02:56 CEST 2013


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-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Jennifer Tickner
Sent: Mittwoch, 26. Juni 2013 10:18
To: r-help at 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 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.



More information about the R-help mailing list