[R] Generating these matrices going backwards

Chris Stubben stubben at lanl.gov
Thu Nov 15 23:00:07 CET 2007


Sorry, I wasn't sure what you meant.  This way will return more than one
answer, right?

N<-c(1,2,1,3)
R<-c(1.75,3.5,1.75,1.3125)

## get all 126 combinations of five 0's and four 1's for matrix
cbn<-as.matrix(expand.grid( rep( list(0:1), 9)))
cbn<- cbn[rowSums(cbn)==4,] 

ans<-list()
ctr<-0
## loop through each combination 
for (i in 1:126){
x<-cbn[i,]
## replace 1's with N
x[which(x==1)]<-N
## create matrix
dim(x)<-c(3,3)
## calculate y and new R
y <-sum(x) * x / (rowSums(x)%o%colSums(x)) 
R1<-y[y[1:3,]>0] 
# check if equal to original R
if(identical(R, R1)) ans[[ctr<-ctr+1]]<-x
}
ans


[[1]]
     [,1] [,2] [,3]
[1,]    0    0    1
[2,]    1    0    3
[3,]    0    2    0

[[2]]
     [,1] [,2] [,3]
[1,]    0    0    1
[2,]    0    2    0
[3,]    1    0    3

[[3]]
     [,1] [,2] [,3]
[1,]    0    2    0
[2,]    0    0    1
[3,]    1    0    3


Chris


francogrex wrote:
> 
> Hi, thanks but the way you are doing it is to assign the values of N in
> the x matrix, knowing from the example I have given where they are
> supposed to be. While the assumption is, you ONLY have values of N and R
> and do NOT know where they would be placed in the x and y matrix a-priori,
> but their position has to be derived from only the (N and R) dataframe you
> have.
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Generating-these-matrices-going-backwards-tf4807447.html#a13782676
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list