0

I would like to generate a continuous number with repetitive increment, for example,from 1 to 3. with a repetitive increment of 1 (x 5). so the output will be.

output =

[1
1
1
1
2
2
2
2
2
3
3
3
3
3]

five repetition of 1, then five repetition of 2 and so on.

I tried this code:

a = [1:1:3]
for i = a(:,1:end)
    disp(i+zeros(5,1))
end

I got the same result, however, I can't put the output in one column. Thanks for the help.

Ralden123
  • 19
  • 7
  • `reshape(repmat(a, 5, 1), 5*numel(a), 1)` – HansHirse Jun 05 '19 at 08:59
  • `kron(a',ones(5,1))` – AVK Jun 05 '19 at 09:08
  • @HansHirse how about i want to extend up to N numbers? for example from 1 to 100? – Ralden123 Jun 05 '19 at 09:10
  • @Ralden123 Go for [Luis' answer](https://stackoverflow.com/a/56457424/11089932) (the old [golf](https://en.wikipedia.org/wiki/Code_golf) fanatic), you can set up your `n` there. – HansHirse Jun 05 '19 at 09:11
  • 1
    Possible duplicate of [Repeat copies of array elements: Run-length decoding in MATLAB](https://stackoverflow.com/questions/1975772/repeat-copies-of-array-elements-run-length-decoding-in-matlab) – obchardon Jun 05 '19 at 09:15

1 Answers1

3

Let

n = 3; % number of distinct numbers
s = 2; % starting number
m = 5; % number of repetitions of each number

A couple of options are

output = repelem(s:s+n-1,m);

or

output = ceil(s-1+1/m:1/m:s+n-1);
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147