Repetitive sequence with calculations

3 Ansichten (letzte 30 Tage)
Kyrylo Melnyk
Kyrylo Melnyk am 12 Feb. 2020
Bearbeitet: Stephen23 am 14 Feb. 2020
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!

Akzeptierte Antwort

David Hill
David Hill am 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 Kommentare
Kyrylo Melnyk
Kyrylo Melnyk am 13 Feb. 2020
Thank you so much!
Stephen23
Stephen23 am 14 Feb. 2020
Bearbeitet: Stephen23 am 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))

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by