Re: [R] matrix indexing

From: Martin Lam <tmlammail_at_yahoo.com>
Date: Thu 18 Aug 2005 - 22:32:28 EST


x <- array(1:20, dim = c(4, 5))
ind <- array(c(1:3, 3:1), dim = c(3, 2))

# instead of using ind (pairs of coordinates) for getting the items in the matrix, you can convert it to a list of single coordinates to point to the item in the matrix:
# t = transpose
# nrow = get the number of rows
indices <- t((ind[,2]-1) * nrow(x) + ind[,1])

# remove the ones indicated by indices
# to get an item from y you can do y[value] y <- x[-indices]

# you could transpose y, but I don't know if that's what you want
y <- t(y)

Hope this helps,

Martin

> Dear R-users,
>
> I was wondering for the following:
>
> Let 'x' be a matrix and 'ind' and indicator matrix,
> i.e.,
>
> x <- array(1:20, dim = c(4, 5))
> ind <- array(c(1:3, 3:1), dim = c(3, 2))
>
> I'd like to get (as a vector) the elements of 'x'
> which are not indexed by 'ind'. Since negative
> indices
> are not allowed in index matrices I thought of using
> something like:
>
> x[ind] <- NA
> x[!is.na(x)]
>
> but are there any more elegant solutions.
>
> Thanks in advance,
> toka



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Aug 18 22:37:35 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:28:58 EST