Julia: How to insert a specific row of matrix inside a specific row of another one

Viewed 53

I have the following matrix:

L = [3 6  18 92 2
     2 24 39 59 3];

I intend to enter the first row of matrix L into the 2nd row of the following matrix:

X = [2 7  43 52 1
     4 21 14 97 4
     3 17 27 85 5];

And the result should be:

Xnew = [2 7  43 52 1
        3 6  18 92 2
        4 21 14 97 4
        3 17 27 85 5];

How can I do that in Julia?

1 Answers

This is a way to do it:

julia> @views [X[1:1, :]; L[1:1, :]; X[2:end, :]]
4×5 Matrix{Int64}:
 2   7  43  52  1
 3   6  18  92  2
 4  21  14  97  4
 3  17  27  85  5

You could get the same without @views but it would be less efficient as it would create intermediate copies of data.

Related