s-news
[Top] [All Lists]

extract from an array of arbitrary dimension

To: <s-news@lists.biostat.wustl.edu>
Subject: extract from an array of arbitrary dimension
From: <Agin.Patrick@hydro.qc.ca>
Date: Fri, 14 Oct 2005 11:07:04 -0400
Thread-index: AcXQ0PATOQXiQ6GSQDmngtjb+R15cA==
Thread-topic: extract from an array of arbitrary dimension

Dear S-Users,

 

I'm trying to extract from an array x of arbitrary dimension in the following way :

 

y <- x[,1] #if x is length(dim(x)==2)

y <- x[,,1] #if x is length(dim(x)==3)

y <- x[,,,1] #if x is length(dim(x)==4)

etc..

 

The only solution I found is to transform x as a vector, extract the first n elements and rearrange the result as an array of appropriate dimensions:

 

v  <- as.vector(x)

lv <- length(v)

d  <- dim(x)

ld <- length(dim(x))

n  <- lv/d[ld]

y  <- array(v[1:n],dim=d[1:(ld-1)])

 

Is there a better way to do that?

Regards,

Patrick

 

<Prev in Thread] Current Thread [Next in Thread>