2013-08-05 5 views
-2

저는 프로그래밍의 초보자이고 25 개의 셀 모두를 최소 거리 "d"로 구분하여 샘플링하려고합니다. 내가 제안을 주셔서 감사합니다 정말 거라고r에있는 거리 조건을 충족하는 샘플 N 셀

b=c()       # vector to store the selected cells 
b[1]=sample(a,1)    # randomly sampling the first cell from my landscape  
x=c() 
for (i in 1:24){      
    b2=sample(a,1)    #propose a candidate cell 
    for(j in 1:i){    
    if (DIJ[b[j],b2]>=d){ #check if the distance between the proposed and ALL the   
    x[i]=1      chosen 
          #cells meets the distance, where DIJ is a distance matrix 
}       assign 1 if it does, 0 otherwise 
    else{x[i]=0    
}} 
    while(sum(x)==i){  #If the sum equals i, then the proposed meets the requirement 
    b2=b[i+1]   against all the cells, otherwise start again with a new  
    }}     proposal 

코드 작업이 조각을해야한다.

알레한드로

+1

A [재현성 예]를 입력 해주세요 (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Thomas

+0

샘플 데이터주세요! – Metrics

+0

샘플 하나의 셀. 샘플링 된 셀과 너무 가까운 셀을 제거하십시오. 샘플 두 번째 셀. 마지막 두 단계를 25 번 반복하십시오. –

답변

1

귀하의 질문은 약간 불분명하다,하지만 난 DIST()에 의해 만들어진 당신이 거리 행렬로 작업 가정하면, 다음이 그것을 할 것입니다. 보다 효율적인 방법이있을 수 있습니다.

library(reshape2) 
m <- matrix(rnorm(100), nrow = 25) 
d <- dist(m, upper=T, diag=T) 

l <- melt(as.matrix(d)) 

# sample 10 values, where value > 1 
l[ sample(which(l$value > 1), 10), ] 
관련 문제