How to sum under the main diagonal without using main diagonal in matrix, in julia?
I was using sum=tril(a)-1 but it doesn't work in julia.
I know I need a mask but I don't know how to use it.
How to sum under the main diagonal without using main diagonal in matrix, in julia?
I was using sum=tril(a)-1 but it doesn't work in julia.
I know I need a mask but I don't know how to use it.
You're looking for the LinearAlgebra module, which is part of the standard library and contains a tril function:
julia> using LinearAlgebra
julia> A = ones(5, 5)
5×5 Array{Float64,2}:
1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0
julia> tril(A, -1)
5×5 Array{Float64,2}:
0.0 0.0 0.0 0.0 0.0
1.0 0.0 0.0 0.0 0.0
1.0 1.0 0.0 0.0 0.0
1.0 1.0 1.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0
julia> sum(tril(A, -1))
10.0
tril function is in the Linear Algebra standard library. To use it, you need to import the LinearAlgebra module. tril takes a second optional argument to specify where you want to start the lower triangular part. You can set it to -1 in order not to take the main diagonal.
using LinearAlgebra
A = rand(5, 5)
lowerA = tril(A, -1)
println(sum(lowerA))
You might also want to take a look at lazy lower triangular view constructors such as LowerTriangular or OneUnitLowerTriangular.