changing a group of numbers in a vector
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
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
0 Comments
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;
2 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!