Can I make a functions input a counter variable for a for loop

3 views (last 30 days)
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
%test script
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
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
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;
AmountWon = 0;
Is there a better way of doing this while making the forloop run a "SlotRounds" number of times??
Ignore the nested forloop
  1 Comment
TADA on 23 Feb 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?

Sign in to comment.

Answers (1)

dpb on 23 Feb 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?');
for i=1:nRounds
% do all your stuff on the arrays here with _i_ as the indexing variable
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! :)


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!