changing a group of numbers in a vector

2 vues (au cours des 30 derniers jours)
Joshua Harrison
Joshua Harrison le 15 Fév 2020
Modifié(e) : Stephen23 le 16 Fév 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

Réponses (2)

Matt J
Matt J le 16 Fév 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 le 16 Fév 2020
Modifié(e) : Sindar le 16 Fév 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 commentaires
Joshua Harrison
Joshua Harrison le 16 Fév 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 le 16 Fév 2020
Modifié(e) : Stephen23 le 16 Fév 2020
Note that square brackets are a concatenation operator, and on two lines of this answer should be replaced by the grouping operator (parentheses):

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by