[R] Inflate/Expand/Resize an array

Robin Hankin r.hankin at noc.soton.ac.uk
Fri Apr 27 13:31:53 CEST 2007


[replying to myself]

it might be better to use

sweep(f,(1:6)[-2],m,"+")

instead.




On 27 Apr 2007, at 11:56, Robin Hankin wrote:

> Hi
>
>
>   f.dims <- c(10,25,1,1,1,14)
>   m.dims <- c(10, 1,1,1,1,14)
>   f <- array(1:prod(f.dims),f.dims)
>   m <- array(1:prod(m.dims),m.dims)
>   jj <- do.call(abind,c(rep(list(m),25),along=2))
>
>
> Then
>
> f + jj
>
> will give you what you want.
>
>
> The more general problem is a bit harder, as you say....
>
>
> HTH
>
> rksh
>
>
> On 27 Apr 2007, at 10:41, Mark Payne wrote:
>
>> Gudday,
>>
>> I've had a good look everywhere trying to figure out how to do
>> this, but
>> I'm afraid I can seem to find an answer anywhere - maybe its
>> because I'm
>> not using the right terms, or maybe its because I'm a dummy. But
>> unfortunately, I am not completely and utterly stuck. Here's the
>> problem:
>>
>> I have two large, six dimensional arrays that I would like to add
>> together. Lets call them f and m. Their dimensions are respectively:
>>
>>> dim(f)
>> [1] 10  25  1  1  1 14
>>> dim(m)
>> [1] 10 1 1 1 1 14
>>
>> You can see that they're pretty close in size, but not identical.
>> What I
>> would like to do is expand or inflate m along its second dimension so
>> that it ends up with dimension 10 25 1 1 1 14, so that I can then add
>> the two together - the new values created along that second dimension
>> would simply be copies of the first value..
>>
>> What's the fastest way to do this? Is there a dedicated function?
>> Ideally I envisage something that you feed the input array, and the
>> desired dimensions, and it does the rest for you. Please also bear in
>> mind that this is a specific problem - the more general case is
>> where I
>> don't know which dimensions are "out of shape", so to speak...
>>
>> I hope that's clear, and that someone can me out here...
>>
>> Cheers,
>>
>> Mark
>>
>> ______________________________________________
>> 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
>> and provide commented, minimal, self-contained, reproducible code.
>
> --
> Robin Hankin
> Uncertainty Analyst
> National Oceanography Centre, Southampton
> European Way, Southampton SO14 3ZH, UK
>   tel  023-8059-7743
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.

--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743



More information about the R-help mailing list