Can I make a functions input a counter variable for a for loop
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
So I have to make an input (SlotRounds) be used in a forloop within my function. The problem is at the same time i'm supposed to make an output (SlotPlayers) have the same number of colums as SlotRounds and the same amount of rows+1.
I can't do this because if I put SlotRounds in my forloop as my counter, it will change the initial value of whatever I set SlotRounds to be, also changing SlotPlayers.
clear all
close all
clc
%test script
%1
SlotRounds = [1:10]
b = zeros(size(SlotRounds));
c = zeros(1,10);
SlotPlayers = [b;c]; %adding row
SlotPlayers = [b; repmat(b,10,1)]; %adding 10 rows
%2
initial_int = randi([5000,25000],1,10);
SlotPlayers(1,:) = initial_int
%3 & 4
Spin = zeros(1,10);
for SlotRounds = 1:10
Spin(1,:) = rand(1,10); %generates a 1x10 column vector of rand. numbers
%5
for SlotRounds = 1:10 %a compares probability to winnings
if Spin <= .99999
AmountWon = 250000;
elseif Spin <= .99985
AmountWon = 100000;
elseif Spin <= .99950
AmountWon = 40000;
elseif Spin <= .99500
AmountWon = 5000;
elseif Spin <= .93000
AmountWon = 500;
else
AmountWon = 0;
end
end
end
Is there a better way of doing this while making the forloop run a "SlotRounds" number of times??
Ignore the nested forloop
1 commentaire
TADA
le 23 Fév 2019
I May be Missing Something or Simply Not Following, but Wouldn't It Be Easier To Simply Call The Loop Index Something Else Than SlotRounds?
Réponses (1)
dpb
le 23 Fév 2019
"So I have to make an input (SlotRounds) be used in a for loop..."
Who says you "have to" do anything, specifically, about how to implement the instructions? The answer to that statement is "No you don't!" :)
As for the Q? "is there a better way?", sure...make the control variables what they are -- the controls, NOT the working variables.
SlotRounds = [1:10];
Don't hardcode stuff like this; can't ever change anything without completely rewriting the code.
nRounds=input('How many rounds to play?');
SlotPlayers=zeros(nRounds);
for i=1:nRounds
...
% do all your stuff on the arrays here with _i_ as the indexing variable
end
NB: the logic test for winnings is not going to work as you wish; read carefully on how Matlab does if tests on arrays. Also, despite that, you seem to have the likelihoods in wrong sense; a whole lot of people are going to win big in your casino--you're likely not going to be able to stay open long! :)
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!