copy!(dest,d,src,so,N)
copy!(dest, do, src, so, N)
Copy N elements from collection src starting at offset so, to array dest starting at offset do. Returns dest.
Examples
-
Copy elements from one array to another:
julia> src = [1, 2, 3, 4]; julia> dest = zeros(Int, 4); julia> copy!(dest, src) 4-element Array{Int64,1}: 1 2 3 4This example copies all elements from the source array
srcto the destination arraydest. -
Copy elements from a vector to a matrix:
julia> vector = [1, 2, 3, 4]; julia> matrix = zeros(Int, 2, 2); julia> copy!(matrix, vector) 2×2 Array{Int64,2}: 1 3 2 4It copies the elements from the vector
vectorto the matrixmatrixcolumn-wise. - Copy elements from one array to a specific range in another array:
julia> src = [5, 6, 7, 8]; julia> dest = [1, 2, 3, 4, 9, 10]; julia> copy!(dest, src[2:3], 5) 6-element Array{Int64,1}: 1 2 3 4 6 7This example copies elements from the source array
srcstarting at index 2 and ending at index 3, to the destination arraydeststarting at index 5.
Common mistake example:
julia> src = [1, 2, 3];
julia> dest = [4, 5];
julia> copy!(dest, src)
ERROR: DimensionMismatch("tried to assign 3 elements to 2 destinations")
In this example, the destination array dest has fewer elements than the source array src. Make sure that the destination array has sufficient space to accommodate all the elements being copied.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.