Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum under main diagonal in julia

Tags:

julia

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.

like image 672
Biljana Radovanovic Avatar asked Dec 17 '22 15:12

Biljana Radovanovic


1 Answers

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
like image 140
pfitzseb Avatar answered Dec 25 '22 14:12

pfitzseb