HI,
I just wonder whether your code worked or not. set.seed(8) mat1<-matrix(sample(1:80,40,replace=TRUE),ncol=8) set.seed(25) mat2<-matrix(sample(1:160,40,replace=TRUE),ncol=8) #Since the dimensions are the same, m<-1:5 n<-1:8 sum1<-0 for(i in 1:length(m)){ for(j in 1:length(n)){ sum1<-sum1+(((mat1[i,j]/d1)-(mat2[i,j]/d2))^2) } } sum1 #[1] 15192.89 #Sara's code: sum((mat1/d1 - mat2/d2)^2) #[1] 15192.89 A.K. ----- Original Message ----- From: Charles Novaes de Santana <charles.sant...@gmail.com> To: "r-help@r-project.org" <r-help@r-project.org> Cc: Sent: Tuesday, December 4, 2012 2:27 PM Subject: Re: [R] Large loops in R Dear Michael, Thank you for your answer. I have 2 matrices. Each position of the matrices is a weight. And I need to calculate the following sum of differences: Considering: mat1 and mat2 - two matrices (each of them 48000 x 48000). d1 and d2 - two constant values. sum<-0; for(i in 1:nrows1){ for(j in 1:nrows2){ sum<-sum+ ( ( (mat1(i,j)/d1) - (mat2(i,j)/d2) )^2 ) } } } I was wondering if there is a better way to do this sum. Thank you for your attention! Best, Charles On Tue, Dec 4, 2012 at 7:54 PM, R. Michael Weylandt <michael.weyla...@gmail.com> <michael.weyla...@gmail.com> wrote: > > > On Dec 4, 2012, at 6:47 PM, Charles Novaes de Santana > <charles.sant...@gmail.com> wrote: > >> Dear all, >> >> I need to access data from a large matrix (48000 x 48000) and to do it >> I am trying to run two loops using "for" command. Surely it is been a >> very slow job. >> >> I heard that "for" is not the best option to perform large loops in R, >> but I don't really know what would be the best (fast) option. sapply? >> vapply? Could anyone help me with this issue, please? > > What exactly are you trying to do? It's likely doable with a few vectorized > operations. > > Michael > > >> >> Thank you very much for your attention and for any help! >> >> Best regards, >> >> Charles >> >> -- >> Um axé! :) >> >> -- >> Charles Novaes de Santana >> http://www.imedea.uib-csic.es/~charles >> PhD student - Global Change >> Laboratorio Internacional de Cambio Global >> Department of Global Change Research >> Instituto Mediterráneo de Estudios Avanzados(CSIC/UIB) >> Calle Miquel Marques 21, 07190 >> Esporles - Islas Baleares - España >> >> Office phone - +34 971 610 896 >> Cell phone - +34 660 207 940 >> >> ______________________________________________ >> R-help@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. -- Um axé! :) -- Charles Novaes de Santana http://www.imedea.uib-csic.es/~charles PhD student - Global Change Laboratorio Internacional de Cambio Global Department of Global Change Research Instituto Mediterráneo de Estudios Avanzados(CSIC/UIB) Calle Miquel Marques 21, 07190 Esporles - Islas Baleares - España Office phone - +34 971 610 896 Cell phone - +34 660 207 940 ______________________________________________ R-help@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. ______________________________________________ R-help@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.