s-news
[Top] [All Lists]

Re: extract from an array of arbitrary dimension

To: Agin.Patrick@hydro.qc.ca
Subject: Re: extract from an array of arbitrary dimension
From: Patrick Burns <pburns@pburns.seanet.com>
Date: Fri, 14 Oct 2005 16:29:21 +0100
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <30258329AD9B574EB1BBFF02CFB7E66B15722A@WPMSXC04.hydroqc.hydro.qc.ca>
References: <30258329AD9B574EB1BBFF02CFB7E66B15722A@WPMSXC04.hydroqc.hydro.qc.ca>
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)
The 'corner' function uses 'do.call' on '[' to perform a
similar task.  The code is available in the Public Domain
Code area of the Burns Statistics website.

The reason for 'corner' being there was not to answer
questions like this, but because it is useful when working
with (largish) matrices and higher dimensional arrays.  It
shows you a small corner of the array so you can see that
it looks like what you expect, or to look at old objects to
see what are in them.

Patrick Burns
patrick@burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Agin.Patrick@hydro.qc.ca wrote:

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>