Displaying 2 results from an estimated 2 matches for "idx4b".
Did you mean:
idx4a
2011 Feb 26
1
Wired behavior of a 2-by-2 matrix indicies
Dear R,
I found a very wired behavior for a 2-by-2 matrix, see this example
> A <- matrix(1:4, 2)
> idx4A <- matrix(1:4, 2)
> A[idx4A]
Error in A[idx4A] : subscript out of bounds
But other matrices are fine,
> B <- matrix(1:9, 3)
> idx4B <- matrix(1:9, 3)
> B[idx4B]
[1] 1 2 3 4 5 6 7 8 9
I can reproduce this for both 32bit windows and 64bit linux with R 2.12.1
and some earlier versions.
Can someone tell me whether this a bug or a feature or something I don't
know?
Thanks!
Feng
--
Feng Li
Department of Statistics
St...
2011 Feb 26
0
Weird behavior of a 2-by-2 matrix indicies
...o 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.
>
> > But other matrices are fine,
> >
> >> B <- matrix(1:9, 3)
> >> idx4B <- matrix(1:9, 3)
> >> B[idx4B]
> > [1] 1 2 3 4 5 6 7 8 9
>
> 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...