Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a matrix with repetitive values from a slice of another matrix

Suppose I have this matrix:

julia> mat=[1 2 3;4 5 6]
2×3 Matrix{Int64}:
 1  2  3
 4  5  6

Now I want to achieve something like this using a standard function:

4×3 Matrix{Int64}:
 1  2  3
 1  2  3
 1  2  3
 1  2  3

Note that the important thing to me is using the slice! here the slice is mat[1, :]. I tried:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

julia> fill(mat[1, :], 4,3)
4×3 Matrix{Vector{Int64}}:
 [1, 2, 3]  [1, 2, 3]  [1, 2, 3]
 [1, 2, 3]  [1, 2, 3]  [1, 2, 3]
 [1, 2, 3]  [1, 2, 3]  [1, 2, 3]
 [1, 2, 3]  [1, 2, 3]  [1, 2, 3]

julia> repeat(mat[1, :], inner=(4,1))
9×1 Matrix{Int64}:
 1
 1
 1
 1
 2
 2
 2
 2
 3
 3
 3
 3

>Solution :

Or in this case just:

julia> repeat(mat[1:1, :], 4)
4×3 Matrix{Int64}:
 1  2  3
 1  2  3
 1  2  3
 1  2  3

(note 1:1 to avoid transposition)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading