# changing a group of numbers in a vector

1 view (last 30 days)
Joshua Harrison on 15 Feb 2020
Edited: Stephen Cobeldick on 16 Feb 2020
Hello,
In this problem, I first need to create a vector that is all zeros for a specific number. 'A=zeros(1,3501);'
Then I need to change vector A so that I have 10 cycles of 1, or 'on' , that last for 50 numbers. For example. A(1:50) will be zero or off, A(51:100)=1 or on; A(101:150)=0 and A(151:200)= 1 etc for 10 'cycles'.
i would need to be able to change the onset of the 'on' value, so that the numbers of 0 in between 1s can be changed. ex: changing the value will make A(101:150)=0 above into A(101:126)=0

Matt J on 16 Feb 2020
As an example,
>> cycles=[0,1,0,1,0,1]; %3 cycles
>> A=repelem(cycles,3)
A =
Columns 1 through 16
0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1
Columns 17 through 18
1 1

Sindar on 16 Feb 2020
Edited: Sindar on 16 Feb 2020
Does this do it or do you need individual cycles to be different lengths?
% total number of elements
N = 3501;
% number of "on" cycles
N_cycles = 10;
% duration of "on" cycles
length_on = 50;
% duration of "off" cycles
length_off = 50;
A=zeros(1,N);
% determine where the "on" cycles start
cycle_on_starts = 1 + length_off + (length_on+length_off)*[0:N_cycles-1];
% create a matrix where each column contains the indexes of "on" for one cycle
cycle_on_inds = cycle_on_starts+[0:length_on-1]';
% flatten into a vector
cycle_on_inds = cycle_on_inds(:);
% remove "on" beyond last element
cycle_on_inds(cycle_on_inds>N) = [];
% set A to "on" for the correct elements
A(cycle_on_inds) = 1; 