'Multiply column of a matrix with row of another matrix in Julia

Let

A = rand(3,3);B = rand(3,3)

I can multiply first column of A : A[:,1] with third row of matrix B : B[3,:] by the command:

reshape(A[:,1],3,1)*reshape(B[3,:],1,3)

to make a 3x3 matrix.

The direct computation

A[:,1]*B[3,:]

is giving the error:

ERROR: MethodError: no method matching *(::Vector{Float64}, ::Vector{Float64})
Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...) at operators.jl:560
  *(::StridedMatrix{T}, ::StridedVector{S}) where {T<:Union{Float32, Float64, ComplexF32, ComplexF64}, S<:Real} at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\LinearAlgebra\src\matmul.jl:44
  *(::StridedVecOrMat{T} where T, ::LinearAlgebra.Adjoint{var"#s832", var"#s831"} where {var"#s832", var"#s831"<:LinearAlgebra.LQPackedQ}) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\LinearAlgebra\src\lq.jl:254
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

Is there any other short/clear method to do this operation?

Answer : A[:, 1:1] * B[3:3, :] and A[:, 1] * B[3, :]' from @phipsgabler's reply. Or view(A, :, 1)*view(B, 3, :)' , @views A[:, 1] * B[3, :]' without copying rows or columns.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source