[R] matrix indexing

Ben Rich richmcb at gmail.com
Thu Aug 18 12:13:48 CEST 2005


Hi,

you might not consider this more elegant, but how about this

x[-apply(ind, 1, function(i) (i[1]-1)*nrow(x) + i[2])]

Ben

On 8/18/05, toka tokas <tokkass at yahoo.com> wrote:
> 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 at 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
>




More information about the R-help mailing list