zeros(A)
zeros(A)
Create an array of all zeros with the same element type and shape as A
.
Examples
julia> zeros(Char,(1,1))
1x1 Array{Char,2}:
'\0'
julia> foo = [1,2];
julia> zeros(foo)
2-element Array{Int64,1}:
0
0
-
Create a 1-dimensional array of zeros with default type:
julia> zeros(5) 5-element Array{Float64,1}: 0.0 0.0 0.0 0.0 0.0
This example creates a 1-dimensional array of zeros with length 5 and default type
Float64
. -
Create a 2-dimensional array of zeros with specified type:
julia> zeros(Int, 2, 3) 2×3 Array{Int64,2}: 0 0 0 0 0 0
It creates a 2-dimensional array of zeros with type
Int
and dimensions 2x3. -
Create a 3-dimensional array of zeros with custom type:
julia> zeros(UInt8, (2, 2, 2)) 2×2×2 Array{UInt8,3}: [:, :, 1] = 0 0 0 0 [:, :, 2] = 0 0 0 0
This example generates a 3-dimensional array of zeros with type
UInt8
and dimensions 2x2x2.
Common mistake example:
julia> zeros(3.5)
ERROR: InexactError: Int64(Int64, 3.5)
In the above example, attempting to create an array of zeros with a non-integer type (3.5
) raises an InexactError
. Ensure that the type provided is valid for creating an array of zeros.
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.