From rksh1 at cam.ac.uk  Fri Dec  5 16:12:17 2008
From: rksh1 at cam.ac.uk (Robin Hankin)
Date: Fri, 05 Dec 2008 15:12:17 +0000
Subject: [Rd] array indexing
Message-ID: <493944D1.7060903@cam.ac.uk>

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



[Rd] array indexing
Robin Hankin