From: Tony Plate <tplate_at_acm.org>

Date: Wed 27 Apr 2005 - 03:27:08 EST

[1,] "b" "l"

[2,] "f" "k"

> replace(J, TRUE, A[cbind(sample(dim(A)[1], length(J), rep=T), sample(dim(A)[2], length(J), rep=T), as.vector(J))])

*> --
*

*> Robin Hankin
*

> Uncertainty Analyst

*> Southampton Oceanography Centre
*

*> European Way, Southampton SO14 3ZH, UK
*

*> tel 023-8059-7743
*

*>
*

*> ______________________________________________
*

*> 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
*

>

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 Wed Apr 27 03:31:29 2005

Date: Wed 27 Apr 2005 - 03:27:08 EST

I'm assuming what you want to do is randomly sample from slices of A selected on the 3-rd dimension, as specified by J. Here's a way that uses indexing by a matrix. The cbind() builds a three column matrix of indices, the first two of which are randomly selected. The use of replace() is to make the result have the same attributes, e.g., dim and dimnames, as J.

> A <- array(letters[1:12],c(2,2,3)) > J <- matrix(c(1,2,3,3),2,2) > replace(J, TRUE, A[cbind(sample(dim(A)[1], length(J), rep=T), sample(dim(A)[2], length(J), rep=T), as.vector(J))]) [,1] [,2]

[1,] "b" "l"

[2,] "f" "k"

> replace(J, TRUE, A[cbind(sample(dim(A)[1], length(J), rep=T), sample(dim(A)[2], length(J), rep=T), as.vector(J))])

[,1] [,2]

[1,] "b" "l"

[2,] "h" "i"

> replace(J, TRUE, A[cbind(sample(dim(A)[1], length(J), rep=T),
sample(dim(A)[2], length(J), rep=T), as.vector(J))])

[,1] [,2]

[1,] "c" "l"

[2,] "h" "k"

>

- Tony Plate

Robin Hankin wrote:

> Hello Juhana

*>
**> try this (but there must be a better way!)
**>
**>
**>
**> stratified.select <- function(A,J){
**> out <- sapply(J,function(i){sample(A[,,i],1)})
**> attributes(out) <- attributes(J)
**> return(out)
**> }
**>
**> A <- array(letters[1:12],c(2,2,3))
**> J <- matrix(c(1,2,3,3),2,2)
**>
**>
**> R> stratified.select(A,J)
**> [,1] [,2]
**> [1,] "b" "i"
**> [2,] "g" "k"
**> R> stratified.select(A,J)
**> [,1] [,2]
**> [1,] "d" "j"
**> [2,] "f" "l"
**> R>
**>
**>
**> best wishes
**>
**> Robin
**>
**>
**>
**>
**> On Apr 26, 2005, at 05:16 am, juhana vartiainen wrote:
**>
*

>> Hi all >> >> Suppose I have a dim=c(2,2,3) matrix A, say: >> >> A[,,1]= >> a b >> c d >> >> A[,,2]= >> e f >> g h >> >> A[,,3]= >> i j >> k l >> >> Suppose that I want to create a 2x2 matrix X, which picks elements >> from the above-mentioned submatrices according to an index matrix J >> referring to the "depth" dimension: >> J= >> 1 3 >> 2 3 >> >> In other words, I want X to be >> X= >> a j >> g l >> >> since the matrix J says that the (1,1)-element should be picked from >> A[,,1], the (1,2)-element should be picked from A[,,3], etc. >> >> I have A and I have J. Is there an expression in A and J that creates X? >> >> Thanks >> >> Juhana >> >> juhana@fief.se >> >> -- >> Juhana Vartiainen >> >> docent in economics >> Director, FIEF (Trade Union Foundation for Economic Research, >> Stockholm), http://www.fief.se >> gsm +46 70 360 9915 >> office +46 8 696 9915 >> email juhana@fief.se >> homepage http://www.fief.se/staff/Juhana/index.html >> >> ______________________________________________ >> 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 >> >>

> Uncertainty Analyst

>

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 Wed Apr 27 03:31:29 2005

*
This archive was generated by hypermail 2.1.8
: Fri 03 Mar 2006 - 03:31:26 EST
*