Using fill () in an array while maintaining the overall structure in Julia

For example, if we have

a=[1 2;3 4]

We want to repeat each element (in a separate column) 2 times, as a result we get:

[1 1 2 2;3 3 4 4]

I tried

hcat([fill(a[n],2) for n=1:length(a)]...)

but returned

[1 3 2 4;1 3 2 4]

Any thoughts on how to achieve the desired results?

Thank you for your help!

+3
source share
1 answer

Use function repeat:

julia> repeat(a, inner=(1, 2))
2×4 Array{Int64,2}:
 1  1  2  2
 3  3  4  4

The keyword innerindicates that you want to repeat the elements (not the entire array), but (1,2)indicates that the rows are not repeated, but the columns are duplicated.

+5
source

Source: https://habr.com/ru/post/1661231/


All Articles