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".
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

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

추가 답변 (0개)

카테고리

질문:

2018년 10월 12일

댓글:

2023년 1월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by