Repetitive sequence with calculations

1 view (last 30 days)
Hello all,
I am trying to create the following sequence of numbers:
5 6 7 5 6 7 5 6 7 8 9 10 8 9 10 8 9 10 11 12 13 ...
To be more specific, 5 6 7 will be repeated three times and then +1 to each element and repeat another three times. The amount of iterations would be pre-defined f.e. n times.
This is what I tried but it didn't work:
a=[5 6 7]
x = []
n=9
for i=1:n
s=3+a
for j=1:3
m(i,j)=s(i)
end
b=s+3
x = [x,b];
end
Huge thanks in advance!

Accepted Answer

David Hill
David Hill on 12 Feb 2020
a=[5 6 7];
x=zeros(1,9*n);
for j=1:n
x(9*(j-1)+1:9*j)=repmat(a,1,3);
a=a+3;
end
  4 Comments
Stephen23
Stephen23 on 14 Feb 2020
Edited: Stephen23 on 14 Feb 2020
Simpler without a loop, here using repelem:
>> L = numel(M);
>> x = repmat(M,1,L*N) + repelem(L*(0:N-1),L*L)
or using kron:
>> x = repmat(M,1,L*N) + kron(0:N-1,L*ones(1,L*L))

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!