Julia中的下三角矩阵 [英] Lower triangular matrix in julia

查看:31
本文介绍了Julia中的下三角矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的列数等于行数.并且对角线等于零.如何构建这个矩阵?

I have the number of columns equals the number of rows. And the the diagonal is is equal to zero. How can I build this matrix?

#mat
#     [,1] [,2] [,3] [,4]
#[1,]   0   NA   NA   NA
#[2,]    1   0   NA   NA
#[3,]    2    4   0   NA
#[4,]    3    5    6   0

我试过了

x=rand(4,4)
4x4 Array{Float64,2}:
 0.60064   0.917443  0.561744   0.135717 
 0.106728  0.72391   0.0894174  0.0656103
 0.410262  0.953857  0.844697   0.0375045
 0.476771  0.778106  0.469514   0.398846 

c=LowerTriangular(x)

4x4 LowerTriangular{Float64,Array{Float64,2}}:
 0.60064   0.0       0.0       0.0     
 0.106728  0.72391   0.0       0.0     
 0.410262  0.953857  0.844697  0.0     
 0.476771  0.778106  0.469514  0.398846

但我正在寻找这样的东西

but l'm looking for something like this

c=LowerTriangular(x)
4x4 LowerTriangular{Float64,Array{Float64,2}}:
 0.0   NA      NA       NA    
 0.106728  0.0    NA      NA    
 0.410262  0.953857  0.0 NA     
 0.476771  0.778106  0.469514  0

对角线应该为零.

推荐答案

这里的灵感来自 Stefan Karpinski 在 Julia 用户的 列表:

Here is something taking inspiration from Code by Stefan Karpinski on the Julia User's list:

function vec2ltri_alt{T}(v::AbstractVector{T}, z::T=zero(T))
    n = length(v)
    v1 = vcat(0,v)
    s = round(Int,(sqrt(8n+1)-1)/2)
    s*(s+1)/2 == n || error("vec2utri: length of vector is not triangular")
    s+=1
    [ i>j ? v1[round(Int, j*(j-1)/2+i)] : (i == j ? z : NaN) for i=1:s, j=1:s ]
end

julia> vec2ltri_alt(collect(1:6))
4x4 Array{Any,2}:
 0  NaN  NaN  NaN
 1    0  NaN  NaN
 2    3    0  NaN
 3    4    6    0

注意:如果需要,请查看官方文档关于三元运算符,以便更清楚地了解 发生了什么?... : 语法在这里.

Note: If desired, check out the official documentation on the ternary operator for a bit more clarity on what is going on with the ? ... : syntax here.

对于那些寻找更标准"的对角矩阵解决方案的人:

这是一个创建更标准解决方案的版本:

Here is a version that creates a more standard solution:

function vec2ltri{T}(v::AbstractVector{T}, z::T=zero(T))
    n = length(v)
    s = round(Int,(sqrt(8n+1)-1)/2)
    s*(s+1)/2 == n || error("vec2utri: length of vector is not triangular")
    [ i>=j ? v[round(Int, j*(j-1)/2+i)] : z for i=1:s, j=1:s ]
end

a = vec2ltri(collect(1:6))

julia> a = vec2ltri(collect(1:6))
3x3 Array{Int64,2}:
 1  0  0
 2  3  0
 3  4  6

julia> istril(a)  ## verify matrix is lower triangular
true

如果您想要上三角: 而不是下三角,只需将 i<=j 更改为 i>=j.

If you want upper triangular: instead of lower, just change the i<=j to i>=j.

其他随机工具注意还有像 tril!(a) 这样的函数,它将给定矩阵原地转换为下三角形,用零替换主对角线以上的所有内容.有关此功能的更多信息,请参阅 Julia 文档作为各种其他相关工具.

Other random tools Note also functions like tril!(a) which will convert in place a given matrix to lower triangular, replacing everything above the main diagonal with zeros. See the Julia documentation for more info on this function, as well as various other related tools.

这篇关于Julia中的下三角矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆