Repetitive sequence with calculations

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!

 採用された回答

David Hill
David Hill 2020 年 2 月 12 日

0 投票

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 件のコメント

Kyrylo Melnyk
Kyrylo Melnyk 2020 年 2 月 12 日
編集済み: Kyrylo Melnyk 2020 年 2 月 12 日
David,
Thanks a lot!
Sorry to bother you but I have another question. If I want to increase the amount of numbers to [5 6 7 8] and then +1 to each element, repeat them 4 times (repeat those steps N times). Basically, what I am trying to say is that I want to change the width (f.e. variable M) of that repetitive component but it doesn't work if I just change '3' to '4'. This is what I tried to do:
n=9;
x=[]
a=[5 6 7 8];
x=zeros(1,9*n);
for j=1:n
x(9*(j-1)+1:9*j)=repmat(a,1,4);
a=a+4;
end
In another words what I really want:
  1. repeat M components for M-times ( 1 2 1 2 next sequence is 3 4 3 4 ..)
  2. add +M to each element after the first step
  3. Repeat this whole process for N times ( f.e. I want to repeat steps 1 and 2 for 15 times)
David Hill
David Hill 2020 年 2 月 13 日
N=15;
M=[5 6 7 8];
x=zeros(1,length(M)^2*N);
for j=1:N
x(length(M)^2*(j-1)+1:length(M)^2*j)=repmat(M,1,length(M));
M=M+length(M);
end
Kyrylo Melnyk
Kyrylo Melnyk 2020 年 2 月 13 日
Thank you so much!
Stephen23
Stephen23 2020 年 2 月 14 日
編集済み: Stephen23 2020 年 2 月 14 日
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))

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

質問済み:

2020 年 2 月 12 日

編集済み:

2020 年 2 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by