scale

scale(A, b) scale(b, A)

Scale an array A by a scalar b, returning a new array.

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), returning a new array.

Note: for large A, scale can be much faster than A .* b or b .* A, due to the use of BLAS.

Examples

Scale an array by a scalar:

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

This example scales each element of array A by the scalar value b, resulting in a new array.

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)
3×3 Array{Int64,2}:
  2   4   6
 12  15  18
 28  32  36

In this example, the function scale(A, b) scales each column i of matrix A by b[i], resulting in a new matrix.

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)
3×3 Array{Int64,2}:
  2   4   6
 12  15  18
 28  32  36

In this example, the function scale(b, A) scales each row i of matrix A by b[i], resulting in a new matrix.

Note: The scale function can be more efficient than using element-wise multiplication (A .* b or b .* A) for large arrays due to the use of BLAS.

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.

*Required Field
Details

Checking you are not a robot: