2Cubed
2Cubed

Reputation: 3551

Create three (or higher) dimensional array with Julia

In Julia, the ; may be used to create a two-dimensional array.

julia> [1 2; 3 4]
2x2 Array{Int64,2}:
 1  2
 3  4

Is it possible to use a similar syntax to create a three-dimensional (or higher-dimensional) array? The following works, but I am unsure as to whether or not there is a cleaner, better way.

julia> reshape(collect(1:8), 2, 2, 2)
2x2x2 Array{Int64,3}:
[:, :, 1] =
 1  3
 2  4

[:, :, 2] =
 5  7
 6  8

Upvotes: 4

Views: 1610

Answers (2)

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22255

I suppose the cleanest manual syntax is via the cat command, e.g.:

cat(3, [1 2 ;3 4], [5 6 ; 7 8]);   % concatenate along the 3rd dimension

Upvotes: 11

Alexander Morley
Alexander Morley

Reputation: 4181

I think you want a list comprehension? This will make it easier when you have more complicated arrays to create.

Something like:

[x+1 for x=1:first, y=1:second, z=1:third]

will give a first X second X third dimensional array populated by x+1.

see http://docs.julialang.org/en/release-0.4/manual/arrays/#comprehensions for more info :)

Upvotes: 5

Related Questions