필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Brian Peoples
Brian Peoples 2019년 2월 23일
편집: TADA 2019년 2월 23일
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
  댓글 수: 1
TADA
TADA 2019년 2월 23일
편집: TADA 2019년 2월 23일
You guys May Want To Read The Answers To Your Classmates questions.

댓글을 달려면 로그인하십시오.

채택된 답변

Brian Hart
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개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by