Randomly Sample entries of a matrix and return the (Row, Column) Indexes in R? -
i have matrix m rows , n columns. need randomly sample different locations in these matrix , return row indexes , col indexes.
my approach: say, want sample 30 percentage of entries in matrix. then, iterate through whole matrix, @ each point, toss biased coin heads of 30 percent probability , select location if heads comes. since, data large, approximately selects 30% of entries. however, observe slow. there way speed up? or better way it?
if m
matrix, try:
arrayind(sample(length(m),0.3*length(m)),dim(m))
an example:
set.seed(1) m<-matrix(ncol=6,nrow=6) arrayind(sample(length(m),0.3*length(m)),dim(m)) # [,1] [,2] #[1,] 4 2 #[2,] 2 3 #[3,] 2 4 #[4,] 6 5 #[5,] 1 2 #[6,] 4 5 #[7,] 5 5 #[8,] 4 6 #[9,] 6 3 #[10,] 2 1
Comments
Post a Comment