Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill a vector in Julia with a repeated list

Tags:

arrays

julia

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.

like image 798
lara Avatar asked Apr 17 '16 22:04

lara


1 Answers

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

like image 76
lara Avatar answered Nov 15 '22 05:11

lara