Skip to content
Prev 251890 / 398498 Next

Wired behavior of a 2-by-2 matrix indicies

Hello,
On Sat, Feb 26, 2011 at 12:11 PM, Feng Li <m at feng.li> wrote:
This shouldn't work. From the help for "[",

          When indexing arrays by ?[? a single argument ?i? can be a
          matrix with as many columns as there are dimensions of ?x?;
          the result is then a vector with elements corresponding to
          the sets of indices in each row of ?i?.

Since there are two dimensions to A, and two rows to idx4A, then this
form is used.
The first row of idx4A is c(1, 3) so the first value extracted is
A[1, 3]
which doesn't exist, thus the error message.
The part that surprises me is that this works.
Apparently there is an implicit conversion to vector here because
dim(B) != ncol(idx4B)

What you seem to want is
A[as.vector(idx4A)]
and
B[as.vector(idx4B)]

but you neglected to tell us what you expected the result to be.

Sarah