I have a code but it keep saying "invalid expression. when calling a function or indexing a variable, use parentheses. otherwise check for mismatched delimiters
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
The was working fine and showing the charts and all
prob = RollingDice()
function prob = RollingDice(~) % calculate the probability...
% of obtaining a # combination...
% between 1 and 12
n = 12; % total number of side between both dice
TNrolls = 0;
prob = zeros(1,12); % define a probability between 1 and 12
while TNrolls < n % total is greater than 1 in order to...
% get a number bigger than 0
dice(1) = randi(6); % the number of sides
dice(2) = randi(6);
Sum = dice(1) + dice(2); % sume represents the...
% the #'s from each dice
TNrolls = TNrolls + 2; % for every roll, we add +2...
% to the total for the # of dice
if Sum == 2
prob(2) = TNrolls/n; % depending on the # of roll it takes...
% obtain a sum of 2, is divides by total
% number of possible sides = 12
elseif Sum == 3
prob(3) = TNrolls/n;
elseif Sum == 4
prob(4) = TNrolls/n;
elseif Sum == 5
prob(5) = TNrolls/n;
elseif Sum == 6
prob(6) = TNrolls/n;
elseif Sum == 7
prob(7) = TNrolls/n;
elseif Sum == 8
prob(8) = TNrolls/n;
elseif Sum == 9
prob(9) = TNrolls/n;
elseif Sum == 10
prob(10) = TNrolls/n;
elseif Sum == 11
prob(11) = TNrolls/n;
elseif Sum == 12
prob(12) = TNrolls/n;
end
end
bar(prob)
xlabel('Sum for Roll')
ylabel('Frequency')
title(['Number of Rolls: ',num2str(Sum)])
end
1 comentario
Respuestas (1)
Voss
el 16 de Feb. de 2023
Editada: Voss
el 16 de Feb. de 2023
It runs without error and shows a plot.
Probably there's a syntax error in a nearby part of your code.
prob = RollingDice()
function prob = RollingDice(~) % calculate the probability...
% of obtaining a # combination...
% between 1 and 12
n = 12; % total number of side between both dice
TNrolls = 0;
prob = zeros(1,12); % define a probability between 1 and 12
while TNrolls < n % total is greater than 1 in order to...
% get a number bigger than 0
dice(1) = randi(6); % the number of sides
dice(2) = randi(6);
Sum = dice(1) + dice(2); % sume represents the...
% the #'s from each dice
TNrolls = TNrolls + 2; % for every roll, we add +2...
% to the total for the # of dice
if Sum == 2
prob(2) = TNrolls/n; % depending on the # of roll it takes...
% obtain a sum of 2, is divides by total
% number of possible sides = 12
elseif Sum == 3
prob(3) = TNrolls/n;
elseif Sum == 4
prob(4) = TNrolls/n;
elseif Sum == 5
prob(5) = TNrolls/n;
elseif Sum == 6
prob(6) = TNrolls/n;
elseif Sum == 7
prob(7) = TNrolls/n;
elseif Sum == 8
prob(8) = TNrolls/n;
elseif Sum == 9
prob(9) = TNrolls/n;
elseif Sum == 10
prob(10) = TNrolls/n;
elseif Sum == 11
prob(11) = TNrolls/n;
elseif Sum == 12
prob(12) = TNrolls/n;
end
end
bar(prob)
xlabel('Sum for Roll')
ylabel('Frequency')
title(['Number of Rolls: ',num2str(Sum)])
end
0 comentarios
Ver también
Categorías
Más información sobre Naming Conventions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!