5

I want to just generate a array like this:

a = [1 1 2 2 3 3 4 4 5 5 6 6 ....]
%% or something like this 
a = [1 1 1 .. ktimes 2 2 2 ... ktimes .....]

Can this be done by a single line of code in MATLAB ? I believe several answers exist. No loops please.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
roni
  • 1,443
  • 3
  • 28
  • 49
  • This behaviour is equivalent to the `rep` function in `R`, which is a standard function. It's unfortunate that MATLAB lacks this functionality built-in. It's a very useful function. All methods here (`kron`, `bsxfun`, `reshape`, `floor/ceil`) can also be found in the duplicate answer. I'm not downplaying the effort taken here. It was a very interesting discussion, but for canonical purposes, I have marked the above post as duplicate. – rayryeng Feb 12 '15 at 18:28
  • 1
    @rayryeng It's actually a good find! – Divakar Feb 12 '15 at 18:35
  • @rayryeng I did not know about that. Should I delete my question ? I dont want to be downvoted. My poor rep :( – roni Feb 12 '15 at 18:36
  • 2
    @roni - Absolutely not. Leave it here. You got some great answers from some of the best MATLAB bigs here. I actually enjoyed the discussion and reading through the answers, even though I have seen them before. I have upvoted all answers plus your post :) However, I have marked it as a duplicate simply for canonical purposes. This will also allow other readers to see that this functionality is integrated into other programming languages and platforms, but not MATLAB *(grumble)*... and that we should totally make a stink about it so that it can be (one day) a native function. – rayryeng Feb 12 '15 at 18:43
  • 1
    @rayryeng I knew there was a duplicate question... (actually, there are several). I was just too lazy to search for it, and answering was just too much fun! – Luis Mendo Feb 12 '15 at 19:35
  • @LuisMendo - lol no problem. I knew exactly what to look for though, so I decided to do it. BTW, I did love the discussion below and loved the banter that all of you were engaging in! – rayryeng Feb 12 '15 at 19:38
  • @rayryeng Yes, it was fun. You should have been here! – Luis Mendo Feb 12 '15 at 19:40
  • @LuisMendo - You all took away all of the possible and valid answers lol. I couldn't provide any other new alternatives! – rayryeng Feb 12 '15 at 19:40

2 Answers2

8

With reshape and repmat

reshape(repmat([1:6],k,1),1,[])

With bsxfun -

reshape(bsxfun(@plus,[1:6],zeros(k,1)),1,[])

On popular demand with floor -

floor(1:1/k:6+(k-1)/k)
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • can this be done without using any complicated functions like repmat or reshape ? I was thinking more like using some kind of round ceil or floor functions ? – roni Feb 12 '15 at 17:33
  • 1
    @roni `repmat` and `reshape` are very basic Matlab functions. They are not complicated at all! – Luis Mendo Feb 12 '15 at 17:34
  • I know still I wanted to just use like ceil floor and round. I mean we do generate a series by typing 1:100. So can't that simple statement be modified? – roni Feb 12 '15 at 17:35
  • 1
    @roni Added floor version too. – Divakar Feb 12 '15 at 17:50
  • 1
    @Divakar I liked your "It's actually quite boring" haha that made me laugh thanks for that. – Benoit_11 Feb 12 '15 at 18:12
  • 2
    @Benoit_11 haha yeah, well then it became it a bit challenging with the demand for using `round/floor/ceil` :) – Divakar Feb 12 '15 at 18:14
  • @Divakar thanks guys for giving me so many options. I just posted this for fun. I was wondering are there any more left ? – roni Feb 12 '15 at 18:20
  • 1
    @roni Use `ceil` or `floor` versions, shift down or up by `0.5` and then use `round` to complete the set of `floor/ceil/round` based approaches! – Divakar Feb 12 '15 at 18:21
8

Let n = 6; and k = 2;. Here are some alternatives:

kron(1:n,ones(1,k))

or

ceil(1/k:1/k:n)

or

double(uint64(1:n*k)/k)
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147