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!
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.
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.
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.
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.
A really useful operator to remember in Julia is the “splat”, ...
. In this case you simply want:
arr = Array(Float64, dims...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With