Azzera filtri
Azzera filtri

How to reference each element of a column vector in an if statement

1 visualizzazione (ultimi 30 giorni)
I need to get AmountWon to reflect each element in the column vector spin. Everytime this runs I get AmountWon = 250000 because it is adding all the Spin values in the Spin vector.
For example, I want AmountWon to be different for each of the 10 different spin values... this could be stored in another variable x that replaces the value in each row by the amount won, if any.
x = zeros(11,10)
Slotrounds = 10
for j = 1:SlotRounds
Spin(1,:) = rand(1,10) %generates a 1x10 column vector of rand. numbers
for jj = 1:SlotRounds %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
x = %I want the x values to change in each row as the loop progresses
end

Risposta accettata

Brian Hart
Brian Hart il 23 Feb 2019
Modificato: Brian Hart il 23 Feb 2019
Hi Brian,
You need to use subscripts to index the Spin variable, as well as x. For example,
if Spin(jj) <= .99999
AmountWon(jj) = 250000
end
for x,
x(j,:) = AmountWon
Oh, there's a logic problem too. You're not testing if the Spin is between two values, just if it's less-than. So the first if statement almost always meets the condition. I think you want something more like:
x = zeros(11,10)
SlotRounds = 10
for j = 1:SlotRounds
Spin(1,:) = rand(1,10) %generates a 1x10 column vector of rand. numbers
for jj = 1:SlotRounds %a compares probability to winnings
if (Spin(jj) <= .99999) & (Spin(jj) > .99999)
AmountWon(jj) = 250000
elseif Spin(jj) <= .99985 & (Spin(jj) > .99950)
AmountWon(jj) = 100000
elseif Spin(jj) <= .99950 & (Spin(jj) > .99500)
AmountWon(jj) = 40000
elseif Spin(jj) <= .99500 & (Spin(jj) > .93000)
AmountWon(jj) = 5000
elseif Spin(jj) <= .93000 & (Spin(jj) > .90000) %made up this lower limit
AmountWon(jj) = 500
else
AmountWon(jj) = 0;
end
end
x(j,:) = AmountWon %I want the x values to change in each row as the loop progresses
end

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by