Creating a similar type of array in Julia – similar() Method

The similar() is an inbuilt function in julia which is used to create an uninitialized mutable array with the given element type and size, based upon the given source array.

Syntax:
similar(array, element_type=eltype(array), dims=size(array))

Parameters:

  • array: Specified arrays.
  • element_type: Specified type of elements.
  • dims=size(array): Specified size of elements.

Returns: It returns an uninitialized mutable array with the given element type and size.

Example 1:




# Julia program to illustrate
# the use of Array similar() method
  
# Getting an uninitialized mutable array
# with the given element type and size
A = [1, 2, 3, 4];
similar(A, 2, 4)
similar(2:8)
similar(2:8, 1)
similar(2:8, 1, 4)


Output:

Example 2:




# Julia program to illustrate
# the use of Array similar() method
  
# Getting an uninitialized mutable array
# with the given element type and size
println(similar(trues(2, 6), 3))
println(similar(falses(5), 1, 3))


Output:


Contact Us