How to define multiple functions based on random numbers with equal probabilities?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
ANURAG DEEPAK
el 5 de Abr. de 2022
Comentada: ANURAG DEEPAK
el 6 de Abr. de 2022
Respected Sir,
I am trying to define 3 fuctions and function is selected based on random numbers with equal probabilities. How can I define the functions?
For instance,
out = randsrc(1,1,[1,2,3;(1/3),(1/3),(1/3)]); % used to choose between 1,2,3 based on equal probability.
if out==1
function calculation1(arguments)
%calculation
elseif out==2
function calculation2(arguments)
% calculation
else
function calculation3(arguments)
% calculation
end
end
end
end
0 comentarios
Respuesta aceptada
David Hill
el 5 de Abr. de 2022
Editada: David Hill
el 5 de Abr. de 2022
switch randi(3)
case 1
y=calculation1(arguments);
case 2
y=calculation2(arguments);
case 3
y=calculation3(arguments);
end
function y=calculation1(arguments)
%your calculations
end
function y=calculation2(arguments)
%your calculations
end
function y=calculation3(arguments)
%your calculations
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Random Number Generation 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!