【发布时间】:2021-04-29 13:50:14
【问题描述】:
除了分配一个新向量并用我的矩阵一个一个地填充它的值,我将如何将大小为n x m 的矩阵调整大小/重新填充为大小为n x m 的向量,概括以下示例:
julia> example_matrix = [i+j for i in 1:3, j in 1:4]
3×4 Array{Int64,2}:
2 3 4 5
3 4 5 6
4 5 6 7
julia> res_vect = [2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7]
12-element Array{Int64,1}:
2
3
4
3
4
5
4
5
6
5
6
7
我的一个想法是:
res_vect = Int[]
for j in 1:size(example_matrix,2)
res_vect = vcat(res_vect, example_matrix[:,j])
end
我觉得这不是最佳方式,但我无法解释为什么......
【问题讨论】:
-
顺便说一句,永远不要用
res_vect = []初始化向量,除非你有很好的理由。[]创建一个Vector{Any},这将毒害您连接它的其他数组。写[]是性能的巨大红旗。如果您希望数组包含Ints,则应编写Int[]。 -
谢谢@DNF,我不知道 :)
标签: arrays performance matrix vector julia