similar
similar(array, [element_type=eltype(array)], [dims=size(array)])
Create an uninitialized mutable array with the given element type and size,
based upon the given source array. The second and third arguments are both
optional, defaulting to the given array's eltype
and size
. The dimensions
may be specified either as a single tuple argument or as a series of integer
arguments.
Custom AbstractArray subtypes may choose which specific array type is
best-suited to return for the given element type and dimensionality. If they do
not specialize this method, the default is an Array(element_type, dims...)
.
For example, similar(1:10, 1, 4)
returns an uninitialized Array{Int,2}
since
ranges are neither mutable nor support 2 dimensions:
julia> similar(1:10, 1, 4)
1x4 Array{Int64,2}:
4419743872 4374413872 4419743888 0
Conversely, similar(trues(10,10), 2)
returns an uninitialized BitVector
with two elements since BitArray
s are both mutable and can support
1-dimensional arrays:
julia> similar(trues(10,10), 2)
2-element BitArray{1}:
false
false
Since BitArray
s can only store elements of type Bool
, however, if you
request a different element type it will create a regular Array
instead:
julia> similar(falses(10), Float64, 2, 4)
2x4 Array{Float64,2}:
2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
Examples
julia> similar(1:10, 1, 4)
1x4 Array{Int64,2}:
4419743872 4374413872 4419743888 0
julia> similar(trues(10,10), 2)
2-element BitArray{1}:
false
false
julia> similar(falses(10), Float64, 2, 4)
2x4 Array{Float64,2}:
2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
In the above examples, similar
is used to create uninitialized arrays with specific element types and dimensions based on the given source array.
-
Create an uninitialized array with specific element type and dimensions:
julia> similar(1:10, 1, 4) 1x4 Array{Int64,2}: 4419743872 4374413872 4419743888 0
This example creates a 1x4 uninitialized array of type
Int64
based on the given range. -
Create an uninitialized bit array with specific dimensionality:
julia> similar(trues(10,10), 2) 2-element BitArray{1}: false false
Here,
similar
is used to create a 1-dimensional bit array with 2 elements based on the giventrues(10,10)
array. - Create an uninitialized array with a different element type:
julia> similar(falses(10), Float64, 2, 4) 2x4 Array{Float64,2}: 2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
This example creates a 2x4 uninitialized array of type
Float64
based on the givenfalses(10)
array.
Note: The specific array type returned by similar
depends on the element type and dimensionality, and custom AbstractArray subtypes may specialize this method to provide appropriate array types.
See Also
Array, broadcast, cat, combinations, conj!, digits!, fieldnames, fill, fill!, last, length, maximum, minimum, ones, parent, parentindexes, partitions, permutations, pointer, pointer_to_array, promote_shape, rand!, reshape, scale, similar, sum, sum_kbn, takebuf_array, transpose!, vec, zeros,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.