# Re: [R] Help with multidimensional array

From: Giuseppe <neox86_at_gmail.com>
Date: Tue, 29 Mar 2011 02:56:45 +0200

Dennis,

Thank you anyway.

On Tue, Mar 29, 2011 at 2:21 AM, Dennis Murphy <djmuser_at_gmail.com> wrote:
> Hi:
>
> Is this what you're after?
>
> m <- array(1:27, c(3, 3, 3))
> xx <- 1:3
> Reduce('+', lapply(xx, function(k) xx[k] * m[, , k]))
>
>> m
> , , 1
>
>      [,1] [,2] [,3]
> [1,]    1    4    7
> [2,]    2    5    8
> [3,]    3    6    9
>
> , , 2
>
>      [,1] [,2] [,3]
> [1,]   10   13   16
> [2,]   11   14   17
> [3,]   12   15   18
>
> , , 3
>
>      [,1] [,2] [,3]
> [1,]   19   22   25
> [2,]   20   23   26
> [3,]   21   24   27
>
>> xx <- 1:3
>> Reduce('+', lapply(xx, function(k) xx[k] * m[, , k]))
>      [,1] [,2] [,3]
> [1,]   78   96  114
> [2,]   84  102  120
> [3,]   90  108  126
>
> HTH,
> Dennis
>
>
> On Mon, Mar 28, 2011 at 4:10 PM, Giuseppe <neox86_at_gmail.com> wrote:
>>
>> I have the following situation.
>>
>> H12 is an array of dimension (n,k,m) and hessian_lambda is a numeric
>> of length m.
>>
>> I need to multiply each matrix H12[,,1], H12[,,2], ..., H12[,m] by
>> hessian_lambda[1], hessian_lambda[2], ..., hessian_lambda[m],
>> respectively, and then add the resulting (n,k) matrices. What I am
>> using at the moment is the following code:
>>
>> H <- matrix(0, n, k)
>>  for(j in 1:(m)) {
>>        H <- H + H12[,,j]*hessian_lambda[j]
>>  }
>>
>> Does anybody see a way to speed up this without using loop? (The
>> output needs to be a matrix).
>>
>> Thank you for your time.
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>

R-help_at_r-project.org 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. Received on Tue 29 Mar 2011 - 00:59:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Mar 2011 - 04:10:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.