changing a group of numbers in a vector

2 visualizaciones (últimos 30 días)
Joshua Harrison
Joshua Harrison el 15 de Feb. de 2020
Editada: Stephen23 el 16 de Feb. de 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

Respuestas (2)

Matt J
Matt J el 16 de Feb. de 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
Sindar el 16 de Feb. de 2020
Editada: Sindar el 16 de Feb. de 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 comentarios
Joshua Harrison
Joshua Harrison el 16 de Feb. de 2020
This seems to work perfectly! Thank you so much! The cycles of 'on' need to be the same length but I need the 'off' to be able to be changed!
Stephen23
Stephen23 el 16 de Feb. de 2020
Editada: Stephen23 el 16 de Feb. de 2020
Note that square brackets are a concatenation operator, and on two lines of this answer should be replaced by the grouping operator (parentheses):

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by