Im trying to create a code that will create a matrix with gaps depending on the input sequences

조회 수: 2 (최근 30일)
This is what i currently have:
beam=15; gap=10; N=10; P1=[0:beam+gap:(beam+gap)*N]; P2=P1+15;
W=[P1(1,1):P2(1,1)]
The above gives: P1 = 0 25 50 75 100 125 150 175 200 225 250 P2 = 15 40 65 90 115 140 165 190 215 240 265
W=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
I need W to keep going for N values so for example W=[P1(1,N):P2(1,N)].
So for this example i would want W=[1:15,25:40,50:65,75:90...]

채택된 답변

Ced
Ced 2016년 3월 12일
편집: Ced 2016년 3월 12일
Hi
Do you need P1 and P2? Or only W?
Otherwise, I believe this would do the trick:
beam=15; gap=10; N=10;
total_gap = beam+gap;
% total gap is gap between starting number of each "segment"
W = bsxfun(@plus,0:beam,(0:total_gap:(N-1)*total_gap)')';
% This creates a vector (0 1 2 3 4 5 ... 15 ), copies it in N rows, and
% for row i, adds total_gap*(i-1). The whole thing is transposed, resulting in
% W = [
% 0 25 50 75 100 ... ;
% 1 26 51 76 101 ... ;
% ...
% 15 40 65 90 115 ... ]
% Now, to concatenate all and have a row vector:
W = W(:)';
Cheers
PS: I am assuming that the "1" in W was a typo and you actually want W starting at "0". Otherwise, just delete the first element.

추가 답변 (1개)

Ahmet Cecen
Ahmet Cecen 2016년 3월 12일
If your gap size is always the same for all elements:
gap = 15;
P1 = [0 25 50 75 100 125 150 175 200 225 250];
P1 = repmat(P1,[gap,1]);
W = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
W = repmat(W',[1 size(P1,2)]);
W = W+P1;
W = W(:)';

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by