Can anyone fix my MATLAB code? I\'m getting errors in line 1 coinmatrix = [(rand
ID: 1717060 • Letter: C
Question
Can anyone fix my MATLAB code? I'm getting errors in line 1
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
the first line is syntactically correct.
if u are getting error at that line then just change the name of your matlab file. Sometimes if the function name is same as the m-file name the code will show some or the other error. Please check.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.