Using push! to add a new row of data

BTW, using global is discouraged (in all programming languages, not just Julia). Instead, you can put your code inside a function.

Here’s one way:

function mycat(X, Y)
    LL = similar(X, 0, 2)
    for i in eachindex(X, Y)
        LL = [LL; [X[i] Y[i]]]
    end
    return LL
end

But here’s a much better way:

function mycat2(X, Y)
    LL = similar(X, length(X), 2)
    for i in eachindex(X, Y)
        LL[i, 1] = X[i]
        LL[i, 2] = Y[i]
    end
    return LL
end

The last one is 200.000 times faster, and runs in 80us instead of 16 seconds.

2 Likes