How to reference each element of a column vector in an if statement
5 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Brian Hart
2019-2-23
编辑:Brian Hart
2019-2-23
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!