Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: calling Array() with an array of dimensions

Tags:

julia

Suppose I want to create a multidimensional array whose dimensions / size per dimension are specified in an array. I want to do something like this:

dims = [2,5,6] # random example, the idea is I don't know dims ahead of time
arr = Array(Float64, dims)

That is not allowed. In the above case one should use:

arr = Array(Float64, dims[1], dims[2], dims[3] )

I don't know the length of dims ahead of time, so the above solution doesn't work for me. Is there a clean workaround outside of using some nasty sprintfs and eval?

Thanks!

like image 488
Mageek Avatar asked May 14 '14 20:05

Mageek


People also ask

How do you create an array of arrays in Julia?

An Array in Julia can be created with the use of a pre-defined keyword Array() or by simply writing array elements within square brackets([]). There are different ways of creating different types of arrays.

How do you find the dimensions of a matrix in Julia?

The size() is an inbuilt function in julia which is used to return a tuple containing the dimensions of the specified array. This returned tuple format is (a, b, c) where a is the rows, b is the columns and c is the height of the array.

How do you find the element of an array in Julia?

Using findall() method The findall() method is used to find all the occurrences of the required element from an array, and return an Array (or CartesianArray, depending on the dimensions of input array) of the index, where the elements are present in the input array.

How do you fill an array in Julia?

Create an array filled with the value x . For example, fill(1.0, (10,10)) returns a 10x10 array of floats, with each element initialized to 1.0 . If x is an object reference, all elements will refer to the same object. fill(Foo(), dims) will return an array filled with the result of evaluating Foo() once.


1 Answers

A really useful operator to remember in Julia is the “splat”, .... In this case you simply want:

arr = Array(Float64, dims...)
like image 54
one-more-minute Avatar answered Oct 13 '22 01:10

one-more-minute