Thank you Bert. Yes, your suggestion is correct and there is no need to pre-define the matrix and the sapply function works quite fast. This resolves my issue. Thank you both againHC
On Sunday, 3 July 2016 11:38 AM, Bert Gunter <bgunter.4...@gmail.com> wrote: Well, yes, ... but no: there is no need to pre-define the matrix. The following is still a (interpreted) loop, but it is fast and short. ## ex is the downloaded array, here filled with random numbers reqX = c(35,35,40,65,95) reqY = c(2,5,10,112,120) out <-sapply(seq_along(reqX), function(i)ex[reqX[i],reqY[i],] ) > dim(out) [1] 365 5 You might find it useful to go through a (web) tutorial or two to learn more about such R functionality. Useful suggestions can be found here: https://www.rstudio.com/online-learning/#R Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Jul 2, 2016 at 3:43 PM, Hemant Chowdhary via R-help <r-help@r-project.org> wrote: > I am working with a 3-dimensional netCDF file having dimensions of X=100, >Y=200, T=365. > My objective is to extract time vectors of a few specific grids that may not > be contiguous on X and/or Y. > > For example, I want to extract a 5x365 matrix where 5 rows are each vectors > of length 365 of 5 specific X,Y combinations. > > For this, I am currently using the following > > reqX = c(35,35,40,65,95); > reqY = c(2,5,10,112,120,120); > nD = length(reqX) > for(i in 1:nD){ > idX = ncvar_get(nc=myNC, varid="myVar", start=c(reqX[i],reqY[i]), > count=c(1,1)) > if(i==1){dX = idX} else {dX = rbind(dX,idX)} > } > > Is there more elegant/faster way other than to using a For Loop like this? It > seems very slow when I may have to get much larger matrix where nD can be > more than 1000. > > Thank you HC > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.