<html><body><div style="color:#000; background-color:#fff; font-family:arial, helvetica, sans-serif;font-size:12pt"><div><span>Olá grupo!</span></div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span></span> </div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span>Estou trabalhando em uma função, mas sou uma iniciante no R, se vcs puderem me dar dicas sobre como torna-lá mais rápida ou simples, seria ótimo!!</span></div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span></span> </div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span>A função
 visa alocar diferentes valores ao redor de uma célula focal pertencente a uma matriz, ou seja, a partir de uma célula focal, preencher os vizinhos primários (os 8 vizinhos mais próximos), secundários (os próximos 16 vizinhos ao redor), e assim por diante, com diferentes valores de uma função.</span></div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span></span> </div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span>aloj<-read.table("2012_aloj.txt") #matriz de '0' e '1', cada '1' será minha célula focal (CF)</span></div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span></span> </div><div style="color: rgb(0, 0, 0);
 font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span>ep<-expand.grid(1:nrow(aloj),1:ncol(aloj)) #função do R que expande uma matriz, gera nova matriz de 2 colunas, mostrando pra cada célula sua posição x na col 1 e y na col 2<br></div></span><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span>gram<-which(<span>aloj</span>==1,arr.ind = TRUE) #encontra '1's, seleciona as células  focais (CFs), suas posições em uma nova matriz 'gram'</span></div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span></span> </div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color:
 transparent;"><span>pbtp<-array(0,dim=c(nrow(aloj),ncol(aloj),nrow(gram))) # array de 3 dimensões onde serão guardadas os valores dos vizinhos de cada célula focal (VZ), cada matriz é referente a uma CF, uma célula com '1', no exemplo são cerca de 250 CFs e 250 matrizes<br>  </span></div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span>for (k in 1:nrow(gram)) {   # analisa cada CF, cada 'k' é referente a uma CF, no caso cerca de 250 'k's<br>   x<-gram[k,1]  <br>   y<-gram[k,2]    # x,y é a posição da CF na matriz 'aloj'<br>   ps<-numeric(d)    #criar um vetor temporário para alocar os valores gerados no 'for p', valores que serão alocados nos vizinhos, um valor pra VZ primário (VZ1), outro pra secundário (VZ2).
 </span></div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span>   #d é um argumento variável, pode ser d<-5</span></div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span>  </span></div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span>   for(p in 1:d){  # dentro do for k, para gerar os valores dos VZ </span></div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span>      ps[p]<-2.718^(-(0.1*p)) #calcula o valor pra cada cél viz, cada 'p' gera um 'ps', esse 'ps' será o valor dos VZ,
 sendo que ps1=>VZ1, ps2=>VZ2,...</span></div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span><span class="tab">    <span class="tab">    #meu problema maior está aqui, a idéia é colocar cada valor ps[p] no seu local adequado (ps1=>VZ1, ps2=>VZ2,...)</span></span></span></div><span><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><br>      s<-which(ep[,1]>(x-(p+0.1)) & ep[,1]<(x+(p+0.1)) & ep[,2]>(y-(p+0.1)) & ep[,2]<(y+(p+0.1))) # s é um vetor que seleciona as células ao redor da minha CF, para p=1 encontra os VZ1, porém para p=2 encontra os VZ2 e também os VZ1, ele seleciona todo o intervalo...</div><div style="color: rgb(0, 0, 0);
 font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;">      <span class="tab">   </span> #cada 'p' gera um 's' diferente, p=1, 's' com 9 elementos (8 VZ1 e 1 CF), p=2, 's' com 25 elementos (16 VZ2, 8 VZ1 e 1 CF)....</div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;">    <span class="tab">    <span class="tab">  </span></span><br>      for (n in 1:length(s)){ #dentro do for p, para colocar os valores ps[d] nos VZ certos<br> <span class="tab">    </span>   v<-ep[s[n],]      <br>   <span class="tab">    </span> t<-pbtp[v[1,1],v[1,2],k]<br> <span
 class="tab">    </span>   if(t==0){   #só analisa as células com '0'<br> <span class="tab">    </span>     pbtp[v[1,1],v[1,2],k]<-ps[p]<br> <span class="tab">    </span>     } # fecha if</span></div><div style="color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; background-color: transparent;"><span> <span class="tab">    </span> } #fecha for n<br>   } #fecha for p  <br>} #fecha for k</span></div><div> </div><div>Até funciona, mas está muito lento, para o exemplo acima, com 250 CF, são cerca de 30s, mas vou usar isso para até 20.000 CF... se alguém souber como tornar a função mais eficiente, seria ótimo, ou mesmo indicar alguma função do R mesmo que já faça isso.</div><div>Aloj em anexo.
 </div><div> </div><div>Valeu galera!!</div><div> </div><div><font style="font-family: bookman old style, new york, times, serif;" color="#7f7f00" size="2"><b>Clara Luz B. Sant'Anna<br></b></font><font style="font-family: bookman old style, new york, times, serif;" size="2">Bióloga, Bacharel e Licenciada<br>Mestranda em Ecologia - Unicamp<br>Laboratório </font><font style="font-family: bookman old style, new york, times, serif;" size="2">de Biogeografia da Conservação e Mudanças Climáticas</font><font style="font-family: bookman old style, new york, times, serif;" size="2"> - UFG<br>(19) 9233.1062</font></div></div></body></html>