Array Multiplication in Julia

I am trying to do something that, in my opinion, should be relatively simple in Julia, but I cannot find any mention of this problem.

Basically, I have a matrix mxnand a vector nx1. What I would like to do is multiply the vector by the matrix, element-wise, but along the axis, so that each element of the matrix is ​​multiplied.

In numpyfor example, it would be:

np.multiply(array, vector)

Is there any way to do this in Julia?

I tried just expanding the vector to fill the array:

projection = 1:size(matrix)[1]
weight_change = hcat(map(x -> vector, projection))

but it gives something with a type Array{Array{Float64, 2}, 2}when I really just need it Array{Float64, 2}, which means that elementary multiplication will not really work.

Is there a way to fix my approach or fix my listening solution?

+4
1

.*, :

julia> A = [ i + j*im for i=1:3, j=1:4 ]
3x4 Array{Complex{Int64},2}:
 1+1im  1+2im  1+3im  1+4im
 2+1im  2+2im  2+3im  2+4im
 3+1im  3+2im  3+3im  3+4im

julia> v = [1:4]
4-element Array{Int64,1}:
 1
 2
 3
 4

julia> w = [1:3]
3-element Array{Int64,1}:
 1
 2
 3

julia> A .* w
3x4 Array{Complex{Int64},2}:
 1+1im  1+2im  1+3im   1+4im
 4+2im  4+4im  4+6im   4+8im
 9+3im  9+6im  9+9im  9+12im

julia> A .* v'
3x4 Array{Complex{Int64},2}:
 1+1im  2+4im  3+9im   4+16im
 2+1im  4+4im  6+9im   8+16im
 3+1im  6+4im  9+9im  12+16im
+9

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


All Articles