Code Clarification on If statements
이전 댓글 표시
I am looking at Ryan Showden's code on the Monty Hall problem and I am confused about one section.
Some background on the Monty Hall problem: If you are a contestant on a game show. There are three doors, one of which conceals a new car, while the other two are empty. After picking a door, the host opens one door to show that it is empty. The host then gives you the option to switch doors. If you switch you win around 60% of the time but if you dont swtich you win 30% of the time.
Can someone please explain how this piece works in relation to the output and rest of the code?
if PromptMessage == 1
Choice2 = PlayerOption(1);
elseif PromptMessage == 2
Choice2 = PlayerOption(2);
end
I have added the rest of the code below.
Doors = [1 0 0]; %Initial digits for door options, randomized later
W = 0; %Initialized win
L = 0; %Initialized lose
Choice1 = 3; %Initial door choice
PromptMessage = 2; %1=stay with original choice, 2=switch to remaining door
for i = 1:100
%Randomize winning door position
Doors(randperm(numel(Doors))) = Doors;
%Determine winning door position
Win = find(Doors);
%Determine the two losing doors the host could reveal
HostOption = find(Doors==0);
if Choice1 == Win
%
% %Randomize which losing door is revealed by the host
HostRevealRand = datasample(HostOption,1);
%
% %Zero out whichever door the host revealed
HostCheck = HostOption - HostRevealRand;
%
% %Find the door the host did not reveal i.e. whichever one wasn't
% %zeroed out
HostCheckIndex = find(HostCheck);
HostLeftOver = HostOption(HostCheckIndex);
%
% %Determine doors now available to player: original winning choice
% %and a losing doorj
PlayerOption = [Choice1 HostLeftOver];
%Determine door player chose depending on input to PromptMessage
if PromptMessage == 1
Choice2 = PlayerOption(1);
elseif PromptMessage == 2
Choice2 = PlayerOption(2);
end
elseif Choice1 == HostOption(1)
%Determine doors now available to player: original losing choice
%and winning door
PlayerOption = [Choice1 Win];
%Determine door user chose depending on input to PromptMessage
if PromptMessage == 1
Choice2 = PlayerOption(1);
elseif PromptMessage == 2
Choice2 = PlayerOption(2);
end
elseif Choice1 == HostOption(2)
%Determine doors now available to player: original losing choice
%and winning door
PlayerOption = [Choice1 Win];
%Determine door user chose depending on input to PromptMessage
if PromptMessage == 1
Choice2 = PlayerOption(1);
elseif PromptMessage == 2
Choice2 = PlayerOption(2);
end
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Work with Components에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!