<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div><span>Olá Clara,</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal; "><span><br></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal; "><span>Construi uma função em Fortran que acredito que vai ajudar.</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal; "><span><br></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal; "><span>Simulei em uma matrix de 100 x
 100 com aproximadamente 5000 CF e demorou </span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal; "><span>5 seg.</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal; "><span><br></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal; ">A função só funciona em SO linux.</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal; "><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;
 ">Pode ser?</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal; "><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal; "><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal; "><span>Valeu!</span></div><div> </div><div>        Fábio Mathias Corrêa<br><br></div><div>     Universidade Estadual de Santa Cruz<br>Departamento de Ciências Exatas e da Terra - DCET</div><br><br><div>Campus Soane Nazaré de Andrade, km 16 Rodovia Ilhéus-Itabuna<br>CEP 45662-900. Ilhéus-Bahia</div><div><br><br></div><div>Tel.: 73-3680-5076<br>  <div style="font-family: 'times new roman', 'new york',
 times, serif; font-size: 12pt; "> <div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; "> <div dir="ltr"> <font size="2" face="Arial"> <hr size="1">  <b><span style="font-weight:bold;">De:</span></b> clara luz <clara_santanna@yahoo.com.br><br> <b><span style="font-weight: bold;">Para:</span></b> "r-br@listas.c3sl.ufpr.br" <r-br@listas.c3sl.ufpr.br> <br> <b><span style="font-weight: bold;">Enviadas:</span></b> Terça-feira, 21 de Agosto de 2012 21:35<br> <b><span style="font-weight: bold;">Assunto:</span></b> [R-br] Ajuda com função<br> </font> </div> <br><div id="yiv1377551967"><div><div style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); 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></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>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="yiv1377551967tab">    <span class="yiv1377551967tab">    #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="yiv1377551967tab">   </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="yiv1377551967tab">    <span class="yiv1377551967tab">  </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="yiv1377551967tab">    </span>   v<-ep[s[n],]      <br>   <span
 class="yiv1377551967tab">    </span> t<-pbtp[v[1,1],v[1,2],k]<br> <span class="yiv1377551967tab">    </span>   if(t==0){   #só analisa as células com '0'<br> <span class="yiv1377551967tab">    </span>     pbtp[v[1,1],v[1,2],k]<-ps[p]<br> <span class="yiv1377551967tab">    </span>     } # fecha if</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="yiv1377551967tab">    </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></span></div></div></div><br>_______________________________________________<br>R-br mailing list<br><a ymailto="mailto:R-br@listas.c3sl.ufpr.br" href="mailto:R-br@listas.c3sl.ufpr.br">R-br@listas.c3sl.ufpr.br</a><br><a href="https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br"
 target="_blank">https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br</a><br>Leia o guia de postagem (<a href="http://www.leg.ufpr.br/r-br-guia" target="_blank">http://www.leg.ufpr.br/r-br-guia</a>) e forneça código mínimo reproduzível.<br><br> </div> </div>  </div></div></body></html>