Repeat each element in the array N times

3,195 views
Skip to first unread message

Sergey Bartunov

unread,
Mar 4, 2013, 12:49:51 PM3/4/13
to julia...@googlegroups.com
Hi. Suppose I have an array X = [1 2 3 4]. I want to efficiently produce another array: [1 1 1 2 2 2 3 3 3 4 4 4] by repeating N times each element of X. How should I do this?

Douglas Bates

unread,
Mar 4, 2013, 1:04:29 PM3/4/13
to julia...@googlegroups.com
On Monday, March 4, 2013 11:49:51 AM UTC-6, Sergey Bartunov wrote:
Hi. Suppose I have an array X = [1 2 3 4]. I want to efficiently produce another array: [1 1 1 2 2 2 3 3 3 4 4 4] by repeating N times each element of X. How should I do this?

I'm sure there are better ways of doing this but this works

```
julia> oo = ones(Int64,3)
3-element Int64 Array:
 1
 1
 1

julia> vcat({i * oo for i in 1:4}...)
12-element Int64 Array:
```

 

Patrick O'Leary

unread,
Mar 4, 2013, 7:56:06 PM3/4/13
to julia...@googlegroups.com
Not sure if more or less efficient: repmat(X, N, 1)[:]

John Myles White

unread,
Mar 4, 2013, 8:05:15 PM3/4/13
to julia...@googlegroups.com
I would really like to have a trivial alternative for this expression that embraces the fact that Julia has 1D arrays. It's so close to perfect, but the [:] is a little ugly.

 -- John

On Mar 4, 2013, at 7:56 PM, Patrick O'Leary <patrick...@gmail.com> wrote:

repmat(X, N, 1)[:]

Tom Short

unread,
Mar 4, 2013, 8:14:01 PM3/4/13
to julia...@googlegroups.com
DataFrames has `rep` (for repeat, an R function) that I was planning
to add an `each` keyword argument to. Maybe in the meantime we should
have `repeach` or something.

DataFrames also has an EachRepeatedVector type that is useful for
saving space for long vectors with lots of repeats.

Patrick O'Leary

unread,
Mar 4, 2013, 8:33:42 PM3/4/13
to julia...@googlegroups.com
I have a less trivial alternative! Don't try this at home...

julia> using Monads

julia> elrepeat(X, n) = (@mdo MList begin
       x <- MList(X)
       return fill(x,n)
       end).value
# methods for generic function elrepeat
elrepeat(X,n) at none:1

julia> elrepeat([1 2 3 4],3)

12-element Int64 Array:
 1
 1
 1
 2
 2
 2
 3
 3
 3
 4
 4
 4

(I actually found a bug in Monads doing this, so not a complete waste...)

Stefan Karpinski

unread,
Mar 4, 2013, 8:45:50 PM3/4/13
to Julia Users
The obvious thing seems like it would be use a comprehension:

julia> v = randn(3)
3-element Float64 Array:
 -0.89917 
 -0.255539
  1.57712 

julia> [v[div(i,3)+1] for i=0:3*length(v)-1]
9-element Any Array:
 -0.89917 
 -0.89917 
 -0.89917 
 -0.255539
 -0.255539
 -0.255539
  1.57712 
  1.57712 
  1.57712 

Also efficient since it doesn't create an temporary intermediate arrays.

Patrick O'Leary

unread,
Mar 4, 2013, 8:51:02 PM3/4/13
to julia...@googlegroups.com
Ahh, I was wanting to do a comprehension solution but couldn't come up with it. Nice.

John Myles White

unread,
Mar 4, 2013, 9:00:53 PM3/4/13
to julia...@googlegroups.com
I don't know: this seems a lot less concise than repmat() or R's rep().

 -- John

Stefan Karpinski

unread,
Mar 4, 2013, 9:09:14 PM3/4/13
to Julia Users
True, but it's more general – and I didn't need to look anything up :-)

Patrick O'Leary

unread,
Mar 4, 2013, 9:23:47 PM3/4/13
to julia...@googlegroups.com
Easily fixed:

rep(v, n) = [v[div(i,n)+1] for i=0:n*length(v)-1]

Stefan Karpinski

unread,
Mar 4, 2013, 10:07:32 PM3/4/13
to Julia Users
A better version would use iteration instead of indexing although how to get a size hint is an open question.

Daniel Jones

unread,
Mar 5, 2013, 12:40:08 AM3/5/13
to julia...@googlegroups.com
Here's a version with iterators, which is probably not what you want, but amusing nonetheless.

using Iterators

xs = [1 2 3 4]
for x in chain(map(x -> repeat(x, 3), xs)...)
println(x)
end

Sergey Bartunov

unread,
Mar 5, 2013, 4:32:54 AM3/5/13
to julia...@googlegroups.com
Wow! Thanks everyone. It's so cool to see there are so many ways to do this (and more than zero)

понедельник, 4 марта 2013 г., 21:49:51 UTC+4 пользователь Sergey Bartunov написал:
Reply all
Reply to author
Forward
0 new messages