Hi. I have been pondering array indexing via matrices. > a <- array(1:27,rep(3,3)) > index <- matrix(c(1,1,1,1,2,3),2,3,byrow=TRUE) > a[index] [1] 1 22 as expected and documented. But what was the thinking behind the decision to access the array by rows rather than columns? The 'index' matrix is ordered as [1,1,1,2,1,3] and so the extraction is a[index[c(1,3,5)]] for the first element and a[index[c(2,3,6)]] for the second. If the indexing was by columns then we would have <fantasy> > a <- array(1:27,rep(3,3)) > index <- matrix(c(1,1,1,1,2,3),2,3) > a[index] [1] 1 22 </fantasy> Thus the extraction is a[index[1:3]] and a[index[4:6]]. This seems to be a sensible way of going about array indexing. For example, even *defining* the 'index' matrix is simpler. So, what is the thinking behind the behaviour as implemented? I'm asking because I want to understand the thinking behind the decision. rksh -- Robin K. S. Hankin Uncertainty Analyst University of Cambridge 19 Silver Street Cambridge CB3 9EP 01223-764877
Robin Hankin wrote:> Hi. > > I have been pondering array indexing via matrices. > > > a <- array(1:27,rep(3,3)) > > index <- matrix(c(1,1,1,1,2,3),2,3,byrow=TRUE) > > a[index] > [1] 1 22 > > > as expected and documented. But what was the thinking > behind the decision to access the array by rows rather > than columns? > > The 'index' matrix is ordered as [1,1,1,2,1,3] and so > the extraction is a[index[c(1,3,5)]] for the first element > and a[index[c(2,3,6)]] for the second. > If the indexing was by columns then we would have > > <fantasy> > > a <- array(1:27,rep(3,3)) > > index <- matrix(c(1,1,1,1,2,3),2,3) > > a[index] > [1] 1 22 > </fantasy> > > Thus the extraction is a[index[1:3]] and a[index[4:6]]. > > This seems to be a sensible way of going about array indexing. > For example, even *defining* the 'index' > matrix is simpler. > > So, what is the thinking behind the behaviour as implemented? > > I'm asking because I want to understand the thinking behind the > decision.From my point of view common thinking with matrices and data.frames is that one row corresponds to one observation (that should be extracted). Uwe Ligges> > rksh >