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

  1. 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
    4

    This example copies all elements from the source array src to the destination array dest.

  2. 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  4

    It copies the elements from the vector vector to the matrix matrix column-wise.

  3. 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
    7

    This example copies elements from the source array src starting at index 2 and ending at index 3, to the destination array dest starting 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.

*Required Field
Details

Checking you are not a robot: