changing a group of numbers in a vector

조회 수: 2 (최근 30일)
Joshua Harrison
Joshua Harrison 2020년 2월 15일
편집: Stephen23 2020년 2월 16일
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

답변 (2개)

Matt J
Matt J 2020년 2월 16일
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 2020년 2월 16일
편집: Sindar 2020년 2월 16일
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
Joshua Harrison
Joshua Harrison 2020년 2월 16일
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 2020년 2월 16일
편집: Stephen23 2020년 2월 16일
Note that square brackets are a concatenation operator, and on two lines of this answer should be replaced by the grouping operator (parentheses):

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by