Array with a changing sequence
5 views (last 30 days)
Show older comments
Hi,
I have an array of time representing 2 hours in one second intervals
t=[1:7200].';
I need to generated a sequence matching the following requirements:
1. Within the first 80% of the array - 4 repetitions every 5 minutes [1;1;1;1;0;...];
2. For the next 10% of time - 4 repetitions every 2.5 minutes
3. For the last 10% of time - 1 repetition every 8 seconds
Thank you in advance
2 Comments
Jan
on 1 Mar 2017
What exactly does "4 repetitions every 5 minutes [1;1;1;1;0;...];" mean? What is hidden in the "..."?
Accepted Answer
John BG
on 2 Mar 2017
Hi Y.U.
I like composing signals, so I hope you like this one:
% time frame
t0=7200
nx=[1:1:t0];
% allocating output
x=zeros(1,numel(nx));
% section cycles
t1=5*60
t2=2.5*60
t3=8
% section offsets
offset_t2=floor(.8*t0) % 80% of 7200 seconds = 5760
offset_t3=floor(.9*t0) % 90% of 7200 seconds = 6480
% base pulse shapes
pat1=[1 1 1 1] % 1st base pulse
pat3=1 % 2nd base pulse
% base signals
S01=zeros(1,t1);S01([1:1:length(pat1)])=1 % 1st base signal
S02=zeros(1,t2);S02([1:1:length(pat1)])=1 % 2nd base signal
S03=zeros(1,t3);S03([1:1:length(pat3)])=1 % 3rd base signal
% section patterns
S1=repmat(S01,1,floor(.8*t0/t1)); % generating 1st section, the initial 80% of t0
ns1=[1:1:length(S1)];
S2=repmat(S02,1,floor(.1*t0/t2)); % generating 2nd section, 10% of t0
ns2=[1:1:length(S2)];
ns3=[1:1:t0-offset_t3]; % generating last section before impact
isequal(offset_t3+numel(ns3),t0) % check
S3=repmat(S03,1,numel(ns3)/t3); % generating 3rd section, last 10% of t0
% build output
x(ns1)=S1 % cast in S1
x(offset_t2+ns2)=S2 % cast in S2
x(offset_t3+ns3)=S3 % cast in S3
% check
stem(x)

comment: as pointed out in the stem graph, there are a couple idle gaps, when changing pace. Because of the choice floor() to make sure no overlap, there may be a bit more silence than desired. Let me know if would like to handle section transitions in a different manner.
So, Y.U.
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
0 Comments
More Answers (1)
Jan
on 1 Mar 2017
Edited: Jan
on 2 Mar 2017
Perhaps:
t = (1:7200).'; % Square brackets are not needed here
signal = zeros(1, 7200);
start = 1:300:7200 * 0.8;
signal(start + (0:3).') = 1; % Matlab R2016b auto-expanding
start = 7200*0.8:180:7200*0.9;
signal(start + (0:3).') = 1;
start = 7200*0.9:8:7200;
signal(start) = 1;
For the unneeded square brackets see http://www.mathworks.com/matlabcentral/answers/35676-why-not-use-square-brackets
[EDITED] And if you use an earlier version, use bsxfun:
t = (1:7200).';
signal = zeros(1, 7200);
start = 1:300:7200 * 0.8;
signal(bsxfun(@plus, start, (0:3).')) = 1;
start = 7200*0.8:180:7200*0.9;
signal(bsxfun(@plus, start, (0:3).')) = 1;
start = 7200*0.9:8:7200;
signal(start) = 1;
2 Comments
Walter Roberson
on 2 Mar 2017
With regards to not having the latest version due to budgets: indeed.
If you check postings you will find that I often document the version limitations in my responses, and I often ask which version people have so I can give them a response for their version. I have virtual machine software and a number of different operating systems installed and somewhere around 15 different MATLAB versions installed so that I can give solutions tailored to the user. When I communicate with Mathworks I push them to keep information about earlier versions available.
However, while I am waiting for the user to indicate versions I may well post a solution that uses the newest facilities, when those facilities are relevant and make the solution easier. And I say the version requirements. If the user has that version then the user sees the solution as soon as they check back. If the user has an earlier release then that might be anywhere from MATLAB 5.0 on up and the user will need to convey that to get a version specific solution. If I had just replied back asking their release then an additional round trip would have been added to the case where they do indeed have access to a new enough version.
When a fundamental facility was added and the user has an old release, I am probably not going to take the time to write a work-around package. For example if the user needs facilities from the Computer Vision Toolbox and cannot use them because they are using a version before the toolbox was released (R2013b I think) then I am not going to write an equivalent toolbox for them. That just shifts their costs of doing business on to me. But if it is more a matter of knowing which older routine to call with which parameters, then Yes, I am probably up for that.
See Also
Categories
Find more on Large Files and Big Data 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!