[R] Distance matrices Combinations

arun smartpink111 at yahoo.com
Thu Apr 25 04:21:47 CEST 2013


HI,

Your code is not very clear. 

mata<-m[,c(u)]       # assume that "m" is "mat1" or "w" (as "m" was not defined)

#also assume that "124" as nrow  of each matrix 
# For 10 distance matrices (#10C4)
set.seed(25)
lst1<- lapply(1:10,function(i) dist(matrix(sample(1:50,5*124,replace=TRUE),nrow=124)))
names(lst1)<- seq_along(lst1)
 lst2<-lapply(seq_len(ncol(combn(names(lst1),4))),function(i) {x<-combn(names(lst1),4)[,i];as.matrix((lst1[[x[1]]])^2+(lst1[[x[2]]])^2+(lst1[[x[3]]])^2+(lst1[[x[4]]])^2)})
 X<- do.call(cbind,lapply(lst2,function(x) {w<- sqrt(x);do.call(rbind,lapply(seq_len(nrow(w)),function(i) {r<-matrix(sort(x[i,],index.return=TRUE)$ix,ncol=1); u<- r[2:6,]; mata<-w[,u];b<- matrix(apply(mata,1,mean),ncol=1); e<-sum(abs(b-w[,i]))}))}))
 o<-mean(apply(X,2,sd))
 o
#[1] 268.7831
A.K.

----- Original Message -----
From: eliza botto <eliza_botto at hotmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Wednesday, April 24, 2013 2:07 PM
Subject: [R] Distance matrices Combinations

Dear UseRs,
MY PROBLEM IS A SMALL PIECE OF A REAL BIG AND A COMPLICATED PROBLEM. IF I DELIBERATE IN A VERY SIMPLE WAY THEN ALL I 
WANT IS TO PUT ALL THE POSSIBLE COMBINATIONS OF 75 DISTANCE MATRICES (BY TAKING 4 MATRICES, MORE COMMONLY 75C4), in the following equation.


t<-as.matrix((MAT1)^2+(MAT2)^2+(MAT3)^2+(MAT4)^2+,upper=T,diag=T))

Then "1215450" values of "t"(one for each combination) should one by one be inculcated in the following loop(to calculate the "o" value) and in the end want those 10 combinations of distance matrices which have lowest "o" values.

e <- vector("list", 124)

w<-sqrt(t)

mat1<-w

for (i in 1:124){

r<-matrix(sort(mat1[i,],index.return=TRUE)$ix,ncol=1)

u<-r[c(2,3,4,5,6),1]

mata<-m[,c(u)]                ##(shifted)

amata<-apply(mata,1,mean)

amata<-data.frame(amata)

aavg<-as.matrix(amata, ncol=1)

b<-aavg

e[[i]]<-print(sum(abs(b-m[,i])))

}

x<-do.call(rbind,e)

Y<-x

z <- apply(Y,2,sd)

o<-mean(Y)

Does my question make any scene?
Thanks in advance
Elisa                           
    [[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