[R] matrix indexing
vito muggeo
vmuggeo at dssm.unipa.it
Wed Mar 15 17:05:14 CET 2006
Dear tom,
is the following what you are looking for?
> a=matrix(runif(9),3,3)
> a
[,1] [,2] [,3]
[1,] 0.9484247 0.9765431 0.6169739
[2,] 0.8423545 0.3137295 0.4031847
[3,] 0.6724235 0.1076373 0.2356923
> b<-matrix(sample(c(TRUE,FALSE),size=9,replace=TRUE),3,3)
> b
[,1] [,2] [,3]
[1,] FALSE TRUE TRUE
[2,] TRUE TRUE TRUE
[3,] FALSE FALSE FALSE
> a[b]
[1] 0.8423545 0.9765431 0.3137295 0.6169739 0.4031847
>
best,
vito
tom wright wrote:
> Can someone please give me a pointer here.
> I have two matrices
>
> matA
> A B C
> 1 5 2 4
> 2 2 4 3
> 3 1 2 4
>
> matB
> A B C
> 1 TRUE FALSE TRUE
> 2 FALSE TRUE TRUE
> 3 FALSE FALSE FALSE
>
> how do I extract all the values from matA where the coresponding entry
> in matB == TRUE (or FALSE), perferably in vector form.
>
> Many thanks
> tom
>
> ______________________________________________
> 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
>
--
====================================
Vito M.R. Muggeo
Dip.to Sc Statist e Matem `Vianelli'
Università di Palermo
viale delle Scienze, edificio 13
90128 Palermo - ITALY
tel: 091 6626240
fax: 091 485726/485612
More information about the R-help
mailing list