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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With