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.

 採用された回答

Walter Roberson
Walter Roberson 2018 年 10 月 12 日

0 投票

n = randi(length(t) - 3)
t(n:n+3) = [1 1 0 0]

4 件のコメント

Daud
Daud 2018 年 10 月 15 日
編集済み: Walter Roberson 2018 年 10 月 19 日
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".
Walter Roberson
Walter Roberson 2018 年 10 月 19 日
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 2023 年 1 月 20 日
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
Walter Roberson
Walter Roberson 2023 年 1 月 20 日
randi([11 22])

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by