unable to get desired results

조회 수: 1 (최근 30일)
Prashant Bhagat
Prashant Bhagat 2022년 7월 13일
댓글: Voss 2022년 7월 13일
Please find the below code.
User = zeros(1,10);
for i = 1:10
probability_of_transmission = rand(1);
if probability_of_transmission > p
j = 5;
User(i) = 1;
else
User(i) = 0;
end
end
I just want the output to be somewhat like: User = [ 0 0 0 1 1 1 1 1 0 0 ]. How can i put one more if argument in the for loop so that at the end i will get consecutive ones insetead of ones at random index.
  댓글 수: 4
Abderrahim. B
Abderrahim. B 2022년 7월 13일
and what does that j doing there in your loop ? With using rand to compute probability_of_transmission you can't get the User vector you want.
Prashant Bhagat
Prashant Bhagat 2022년 7월 13일
j is the number of time slots required for transmission of one packet. Then what i have to do to get the desired output

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

채택된 답변

Voss
Voss 2022년 7월 13일
Maybe this?
User = zeros(1,10);
p = 0.785;
for i = 1:10
probability_of_transmission = rand(1);
if probability_of_transmission > p
j = 5;
User(i+(0:j-1)) = 1;
end
end
User
User = 1×10
0 0 0 1 1 1 1 1 0 0
Note that the sequences of 5 ones may overlap each other and may also extend User to beyond 10 elements; it's not clear whether those aspects would be bugs or features for what you want to do.
  댓글 수: 2
Prashant Bhagat
Prashant Bhagat 2022년 7월 13일
Thanks
Voss
Voss 2022년 7월 13일
You're welcome!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by