Can anyone fix my MATLAB code? I\'m getting errors in line 1. But it says after
ID: 1717064 • Letter: C
Question
Can anyone fix my MATLAB code? I'm getting errors in line 1. But it says after running:
Total Heads in a row: [3 32 39 21 5] Undefined function or variable 'CoinToss'.
Error in Matlab (line 36) if CoinToss(x,y) == CoinToss (x-1,y)
coinmatrix = [(randi ([0:1],[100,1])) (randi ([0:1],[100,1])) (randi ([0:1],[100,1])) (randi ([0:1],[100,1])) ];
coinmatrixcount1 = 0;
headsinarow0 = 0;
headsinarow1 = 0;
headsinarow2 = 0;
headsinarow3 = 0;
headsinarow4 = 0;
headscol = sum(coinmatrix);
totalheads = sum(headscol);
totalofonesinrow = sum(coinmatrix,2);
Runs = zeros(1,10);
for i = 1:100;
if totalofonesinrow(i) == 0
headsinarow0 = (headsinarow0 +1);
elseif totalofonesinrow(i) == 1
headsinarow1 = (headsinarow1 +1);
elseif totalofonesinrow(i) == 2
headsinarow2 = (headsinarow2 +1);
elseif totalofonesinrow(i) == 3
headsinarow3 = (headsinarow3 +1);
elseif totalofonesinrow(i) == 4
headsinarow4 = (headsinarow4 +1);
end
end
totalheadsinarow = [headsinarow0, headsinarow1, headsinarow2, headsinarow3, headsinarow4]';
display(['Total Heads in a row: [' num2str(totalheadsinarow') ']'])
for y = 1:4
Length = 0;
for x = 1:100
if x ==1; Length = 1;
else
if CoinToss(x,y) == CoinToss (x-1,y)
Length = (Length +1);
else
Runs(Length) = Runs(Length) + 1;
Length = 1;
end
end
end
Runs(Length) = Runs(Length) + 1;
end
BDistribution = [6,25,38,25,6]';
display(['Binomial Distribution: [' num2str(BDistribution') '];'])
Runs;
display(['Runs: [' num2str(Runs) ']'])
result = [Bdistribution, totalheadsinarow];
subplot(1, 2, 1)
bar(RunsLength)
subplot(1, 2, 2)
bar(0:4 *[totalheadsinarow, BDistribution], 'grouped');
Explanation / Answer
Solution :
the follwoing bugs were identified in you code:
you havenot declared "CoinToss" any where and you are using it for conditional operation.
in the case i defined it in the start. i can still edit if if you let me know the exactpurpose of your code.
(for time i simply initialised it with coinmatrix)!
other errors are :
BDistributions are mistyped as " Bdistributions" , please note that matlab is case sensitive.
here : variable "a" and "A" are completely different .
also :
bar(RunsLength) is not a variable. you must enter
either : bar(Runs(Length)) or bar(Runs) giving you two different outputs .(go for what yourcode needs!)
so the edited code is :
%******************************************************************
clear all;
clc;
coinmatrix = [(randi ([0:1],[100,1])) (randi ([0:1],[100,1])) (randi ([0:1],[100,1])) (randi ([0:1],[100,1])) ];
CoinToss = coinmatrix; %********************************edited
coinmatrixcount1 = 0;
headsinarow0 = 0;
headsinarow1 = 0;
headsinarow2 = 0;
headsinarow3 = 0;
headsinarow4 = 0;
headscol = sum(coinmatrix);
totalheads = sum(headscol);
totalofonesinrow = sum(coinmatrix,2);
Runs = zeros(1,10);
for i = 1:100;
if totalofonesinrow(i) == 0
headsinarow0 = (headsinarow0 +1);
elseif totalofonesinrow(i) == 1
headsinarow1 = (headsinarow1 +1);
elseif totalofonesinrow(i) == 2
headsinarow2 = (headsinarow2 +1);
elseif totalofonesinrow(i) == 3
headsinarow3 = (headsinarow3 +1);
elseif totalofonesinrow(i) == 4
headsinarow4 = (headsinarow4 +1);
end
end
totalheadsinarow = [headsinarow0, headsinarow1, headsinarow2, headsinarow3, headsinarow4]';
display(['Total Heads in a row: [' num2str(totalheadsinarow') ']'])
for y = 1:4
Length = 0;
for x = 1:100
if x ==1; Length = 1;
else
if CoinToss(x,y) == CoinToss (x-1,y)
Length = (Length +1);
else
Runs(Length) = Runs(Length) + 1;
Length = 1;
end
end
end
Runs(Length) = Runs(Length) + 1;
end
BDistribution = [6,25,38,25,6]';
display(['Binomial Distribution: [' num2str(BDistribution') '];'])
Runs;
display(['Runs: [' num2str(Runs) ']'])
result = [BDistribution, totalheadsinarow]; %********************************edited
subplot(1, 2, 1)
bar(Runs(Length)) %********************************edited
subplot(1, 2, 2)
bar(0:4 *[totalheadsinarow, BDistribution], 'grouped');
%*******************************************************************************************