在Julia的矩阵中添加一行?

bec*_*cko 2 julia

如何在Julia中向矩阵添加行?

例如,

mat = [1 2 3; 3 4 2]
Run Code Online (Sandbox Code Playgroud)

我想x = [4 2 1]在最后添加行.我试过了:

push!(mat, x)
Run Code Online (Sandbox Code Playgroud)

但它给出了一个错误.

小智 6

对于矩阵的连接,您可以这样做:

mat = [mat;x]
Run Code Online (Sandbox Code Playgroud)

或者使用函数垂直连接:

vcat(mat,x)
Run Code Online (Sandbox Code Playgroud)

文档中阅读有关这些操作的更多信息.

  • 也许返回新矩阵并分配外部函数将是一种良好的形式,因为它也会暴露变化而不是将它们隐藏在副作用中. (4认同)