Put a given sequence on time slots

For example, I have 48-time slots (t) and I want to put a block of a sequence [1,1,0,0] on these time slots randomly from (t+n) to (t+n+4). How can i generate such a sequence.

 Réponse acceptée

Walter Roberson
Walter Roberson le 12 Oct 2018
n = randi(length(t) - 3)
t(n:n+3) = [1 1 0 0]

4 commentaires

Daud
Daud le 15 Oct 2018
Modifié(e) : Walter Roberson le 19 Oct 2018
Thank you very much for the reply>
I could not understand the above code:
Well for example:
d = [1 1 0 0];
m = zeros(12,1);
for t = 1:length(d):12
m(t:t+length(d)-1) = d;
end
I will get [110011001100]
But I want this sequence in random means:
e.g
It might be [011000011000] or [001100110000] e.t.c.
note: I have a fixed block "d" but I want to activate this "d" on random time slot "t".
You did not follow my instructions to use randi. Also, you appear to be wanting to put in multiple copies of the block.
The below code puts in a configurable number of copies of d.
This code makes no attempt to backtrack if it gets stuck, so in theory it could get stuck after placing as few as 7 copies of length 4 into your array of length 48, such as an occupancy pattern of
000111100011110001111000111100011110001111001111
I assume in this code that it is not permitted to overlap even in places where d is 0 -- so, for example, that it is not permitted to generate 1101100 even though after dropping in the first 1100 that the 4th position is 0: I deem that anything initialized with a value from d cannot be written to again.
m = zeros(1, 48);
d = [1 1 0 0];
num_copies = 3; %put d into m this many times
used = false(size(m));
Ld = length(d);
for K = 1 : num_copies
while true
n = randi(length(m) - Ld + 1);
if ~any(used(n:n+Ld-1)); break; end
end
m(n : n + Ld - 1) = d;
used(n : n + Ld - 1) = true;
end
Hiba Haider
Hiba Haider le 20 Jan 2023
hello dear
same idea i have 24 slot for example i want choose one slot from slot 11 to slot 22 how can i apply

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by