如何使朱莉娅矩阵的列标准化

发布于 2025-02-07 13:32:23 字数 76 浏览 2 评论 0原文

给定一个尺寸m,n的矩阵a,如何通过朱莉娅中的某个函数或其他过程使该矩阵的列标准化(目标是将列正常化,以使我们的新矩阵具有长度为1的列)?

Given a matrix A of dimensions m,n, how would one normalize the columns of that matrix by some function or other process in Julia (the goal would be to normalize the columns of A so that our new matrix has columns of length 1)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不乱于心 2025-02-14 13:32:23

Mapslices似乎在性能方面存在一些问题。在我的计算机(和v1.7.2)上,这是一个更快的速度:

x ./ norm.(eachcol(x))'

这是一个就地版本(因为everycol创建视图),它仍然更快(但仍然分配一点)

normalize!.(eachcol(x))

: ,某些比映射的循环版本的5x3矩阵:

# works in-place:
function normcol!(x)
    for col in eachcol(x)
        col ./= norm(col)
    end
    return x
end
# creates new array:
normcol(x) = normcol!(copy(x))

edit:添加了一个单线分配:

foreach(normalize!, eachcol(x))

这不分配任何内容,与归一化!。是, foreach 没有返回任何内容,这使得在不需要输出的情况下它有用。

mapslices seems to have some issues with performance. On my computer (and v1.7.2) this is 20x faster:

x ./ norm.(eachcol(x))'

This is an in-place version (because eachcol creates views), which is faster still (but still allocates a bit):

normalize!.(eachcol(x))

And, finally, some loop versions that are 40-70x faster than mapslices for the 5x3 matrix:

# works in-place:
function normcol!(x)
    for col in eachcol(x)
        col ./= norm(col)
    end
    return x
end
# creates new array:
normcol(x) = normcol!(copy(x))

Edit: Added a one-liner with zero allocations:

foreach(normalize!, eachcol(x))

The reason this does not allocate anything, unlike normalize!., is that foreach doesn't return anything, which makes it useful in cases where output is not needed.

面如桃花 2025-02-14 13:32:23

如果您想要一个新矩阵,则mapslices可能是您想要的:

julia> using LinearAlgebra

julia> x = rand(5, 3)
5×3 Matrix{Float64}:
 0.185911  0.368737  0.533008
 0.957431  0.748933  0.479297
 0.567692  0.477587  0.345943
 0.743359  0.552979  0.252407
 0.944899  0.185316  0.375296

julia> y = mapslices(x -> x / norm(x), x, dims=1)
5×3 Matrix{Float64}:
 0.112747  0.327836  0.582234
 0.580642  0.66586   0.523562
 0.344282  0.424613  0.377893
 0.450816  0.491642  0.275718
 0.573042  0.164761  0.409956

julia> map(norm, eachcol(y))
3-element Vector{Float64}:
 1.0
 1.0
 1.0

If you want a new matrix then mapslices is probably what you want:

julia> using LinearAlgebra

julia> x = rand(5, 3)
5×3 Matrix{Float64}:
 0.185911  0.368737  0.533008
 0.957431  0.748933  0.479297
 0.567692  0.477587  0.345943
 0.743359  0.552979  0.252407
 0.944899  0.185316  0.375296

julia> y = mapslices(x -> x / norm(x), x, dims=1)
5×3 Matrix{Float64}:
 0.112747  0.327836  0.582234
 0.580642  0.66586   0.523562
 0.344282  0.424613  0.377893
 0.450816  0.491642  0.275718
 0.573042  0.164761  0.409956

julia> map(norm, eachcol(y))
3-element Vector{Float64}:
 1.0
 1.0
 1.0
陌上芳菲 2025-02-14 13:32:23

如果按长度1,您的意思是与Norm 1一样,这可能会起作用

using LinearAlgebra

# if this is your matrix
m = rand(10, 10)

# norm comes from LinearAlgebra
# or you can define it as 
# norm(x) = sqrt(sum(i^2 for i in x))
g(x) = x ./ norm(x)

# this has columns that have approximately norm 1
normed_m = reduce(hcat, g.(eachcol(m)))

,尽管我不知道有更好的解决方案!

If by length 1 you mean like with norm 1 maybe this could work

using LinearAlgebra

# if this is your matrix
m = rand(10, 10)

# norm comes from LinearAlgebra
# or you can define it as 
# norm(x) = sqrt(sum(i^2 for i in x))
g(x) = x ./ norm(x)

# this has columns that have approximately norm 1
normed_m = reduce(hcat, g.(eachcol(m)))

Though there are likely better solutions I don't know!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文