Create 3d array from 2 2d arrays

Viewed 59

Apologies in advance if this is a really silly question.

I have 2 arrays - StartAmount is a 165×11 Matrix{Float64}: that contains 11 "types" of monetary values for 165 lives, and IncreaseFactors is a 100×11 Matrix{Float64}: that contains 11 increase factors for each of 100 years. I want to multiply these by each other such that my result is a 165x11x100 array, with each of the 11 "types" of amount increased for 100 years. When I try StartAmount .* IncreaseFactors I get ERROR: DimensionMismatch("arrays could not be broadcast to a common size; got a dimension with lengths 165 and 100"). I have already set up a 165x11x100 array of zeros, but I don't know how to populate it!

Any suggestions on how to manipulate my two arrays to create the 3D array I am after?

Thanks!

1 Answers

Tullio.jl can abstract away many indexing notations nicely.

using Tullio

@tullio out[i,j,k] := StartAmount[i,j] * IncreaseFactors[k,j]
165×11×100 Array{Float64, 3}:
[:, :, 1] =
 ...

It essentially does what the following 3-loop would do but without caring about indices:

out = [StartAmount[i,j] * IncreaseFactors[k,j] for i=1:165, j=1:11, k=1:100]
Related