Best way to subtract vector from matrix in Julia

What is the best way to subtract a length vector Nfrom a size matrix (N, K)in Julia?

Of course, the cycle foror repmatshould work, but they do not seem to be the most effective.

Can I use it somehow broadcast?

+4
source share
1 answer
julia> [1 2 3; 4 5 6; 7 8 9] .- [1; 2; 3]
3×3 Array{Int64,2}:
 0  1  2
 2  3  4
 4  5  6

(obviously, the subtraction of horizontal vectors is also translated)

julia> [1 2 3; 4 5 6; 7 8 9] .- [1 2 3]
3×3 Array{Int64,2}:
 0  0  0
 3  3  3
 6  6  6

Also note that the broadcast call .-in the above example is essentially equivalent

julia> (-).([1 2 3; 4 5 6; 7 8 9], [1; 2; 3])
3×3 Array{Int64,2}:
 0  1  2
 2  3  4
 4  5  6

julia 0.6, f.(args)/under-the-hood .
(.. .- , "" -).

+8

Source: https://habr.com/ru/post/1678635/


All Articles