scale!

..  scale!(A, b)
           scale!(b, A)

Scale an array ``A`` by a scalar ``b``, similar to :func:`scale` but
overwriting ``A`` in-place.

If ``A`` is a matrix and ``b`` is a vector, then ``scale!(A,b)``
scales each column ``i`` of ``A`` by ``b[i]`` (similar to
``A*diagm(b)``), while ``scale!(b,A)`` scales each row ``i`` of
``A`` by ``b[i]`` (similar to ``diagm(b)*A``), again operating in-place
on ``A``.

Examples

The scale! function in Julia scales an array A by a scalar b in-place. It can be used to scale both vectors and matrices. Here are some examples of how to use the scale! function:

  1. Scale a vector in-place:

    julia> v = [1, 2, 3, 4];
    julia> scale!(v, 2);
    julia> v
    4-element Array{Int64,1}:
    2
    4
    6
    8

    This example scales each element of the vector v by the scalar value 2.

  2. Scale each column of a matrix by a vector:

    julia> A = [1 2 3; 4 5 6; 7 8 9];
    julia> b = [2, 3, 4];
    julia> scale!(A, b);
    julia> A
    3×3 Array{Int64,2}:
    2   4   6
    12  15  18
    28  32  36

    In this example, the scale! function scales each column of matrix A by the corresponding element of vector b.

  3. Scale each row of a matrix by a vector:
    julia> A = [1 2 3; 4 5 6; 7 8 9];
    julia> b = [2, 3, 4];
    julia> scale!(b, A);
    julia> A
    3×3 Array{Int64,2}:
    2   4   6
    12  15  18
    28  32  36

    This example scales each row of matrix A by the corresponding element of vector b.

Note: The scale! function modifies the input array in-place, meaning the original array is overwritten with the scaled values.

If there are any mistakes made while using the scale! function, it is usually related to passing arrays of incompatible sizes. Make sure the dimensions of the array and scalar/vector match appropriately.

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: