本文介绍了连接 2 个 Julia 数组而不修改它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接 2 个数组.

I would like to concatenate 2 arrays.

julia> l1=["a","b"]
2-element Array{ASCIIString,1}:
 "a"
 "b"

julia> l2=["c","d"]
2-element Array{ASCIIString,1}:
 "c"
 "d"

append! 可以做到这一点,但这个函数正在修改 l1(这是一个以 ! 命名的函数)

append! can do this but this function is modifying l1 (that's a function named with a !)

julia> append!(l1, l2)
4-element Array{ASCIIString,1}:
 "a"
 "b"
 "c"
 "d"

julia> l1
4-element Array{ASCIIString,1}:
 "a"
 "b"
 "c"
 "d"

我正在寻找一个 append 函数(不带感叹号).

I was looking for a append function (without exclamation point).

但是这样的功能好像不存在.

But such a function doesn't seems to exist.

有什么想法吗?

推荐答案

除了@oleeinar 的回答,还可以使用 hcatvcat 来拼接数组:

In addition to @oleeinar's answer, you can use hcat and vcat to concatenate arrays:

l3 = vcat(l1, l2)
4-element Array{ASCIIString,1}:
 "a"
 "b"
 "c"
 "d"

你也可以用 hcat 水平连接:

You can also concatenate horizontally with hcat:

l4 = hcat(l1, l2)
2x2 Array{ASCIIString,2}:
 "a"  "c"
 "b"  "d"

这篇关于连接 2 个 Julia 数组而不修改它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 13:23