Fill a vector in Julia with a repeated list
Solution 1
Thanks Dan Getz for the answer:
repeat([1, 2, 3, 4], outer = 4)
Type ?repeat
at the REPL to learn about this useful function.
In older versions of Julia, repmat
was an alternative, but it has now been deprecated and absorbed into repeat
Solution 2
As @DanGetz has pointed out in a comment, repeat
is the function you want. From the docs:
repeat(A, inner = Int[], outer = Int[])
Construct an array by repeating the entries of
A
. The i-th element ofinner
specifies the number of times that the individual entries of the i-th dimension ofA
should be repeated. The i-th element ofouter
specifies the number of times that a slice along the i-th dimension ofA
should be repeated.
So an example that does what you want is:
X = repeat(G; outer=[k])
where G
is the array to be repeated, and k
is the number of times to repeat it.
I will also attempt to answer your confusion about the result of fill
. Julia (like most languages) makes a distinction between vectors containing numbers and numbers themselves. We know that fill(5, 5)
produces [5, 5, 5, 5, 5]
, which is a one-dimensional array (a vector) where each element is 5
.
Note that fill([5], 5)
, however, produces a one-dimensional array (a vector) where each element is [5]
, itself a vector. This prints as
5-element Array{Array{Int64,1},1}:
[5]
[5]
[5]
[5]
[5]
and we see from the type that this is indeed a vector of vectors. That of course is not the same thing as the concatenation of vectors. Note that [[5]; [5]; [5]; [5]; [5]]
is syntax for concatenation, and will return [5, 5, 5, 5, 5]
as you might expect. But although ;
syntax (vcat
) does concatenation, fill
does not do concatenation.
Mathematically (under certain definitions), we may imagine R^(kn) to be distinct (though isomorphic to) from (R^k)^n, for instance, where R^k is the set of k-tuples of real numbers. fill
constructs an object of the latter, whereas repeat
constructs an object of the former.
lara
Updated on June 08, 2022Comments
-
lara almost 2 years
I would like to create a column vector X by repeating a smaller column vector G of length h a number n of times. The final vector X will be of length h*n. For example
G = [1;2;3;4] #column vector of length h X = [1;2;3;4;1;2;3;4;1;2;3;4] #ie X = [G;G;G;G] column vector of length h*n
I can do this in a loop but is there an equivalent to the 'fill' function that can be used without the dimensions going wrong. When I try to use fill for this case, instead of getting one column vector of length h*n I get a column vector of length n where each row is another vector of length h. For example I get the following:
X = [[1,2,3,4];[1,2,3,4];[1,2,3,4];[1,2,3,4]]
This doesn't make sense to me as I know that the ; symbol is used to show elements in a row and the space is used to show elements in a column. Why is there the , symbol used here and what does it even mean? I can access the first row of the final output X by X[1] and then any element of this by X[1][1] for example.
Either I would like to use some 'fill' equivalent or some sort of 'flatten' function if it exists, to flatten all the elements of the X into one column vector with each entry being a single number.
I have also tried the reshape function on the output but I can't get this to work either.