Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

MATLAB algorithm. How do you write this algorithm below in MATLAB?

1 vue (au cours des 30 derniers jours)
Sarah Smith
Sarah Smith le 21 Nov 2019
Clôturé : MATLAB Answer Bot le 20 Août 2021
How do you write this algorithm in MATLAB?
Input p // p=1/2
Begin
Y <-- 0
C <-- 0
For t=1 to 7 do
Begin
if Random {[0,1]} < p then
X(t)=1
else
X(t)=0
if X(t)=1 then
C <-- C+1
else
C <-- 0
if C>= 3 then Y <--1
End
End
Output Y

Réponses (1)

Image Analyst
Image Analyst le 27 Nov 2019
Start with this:
p = input('Enter p '); % E.g. p=0.5
Y = 0; % Y <-- 0
C = 0; % C <-- 0
for t = 1 : 7
if rand < p
X(t) = 1;
else
X(t)=0;
if X(t) == 1
C = C + 1; %C <-- C+1
else
C = 0; % C <-- 0
if C >= 3
Y = 1; % Y <--1
end
end
end
end
% Output Y
Y

Cette question est clôturée.

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by