Quantcast
Viewing all articles
Browse latest Browse all 41

Re: One variable with multiple values depending on conditions

On 04/12/2017 9:22 AM, Edward Mercure wrote:
> I am relatively new to MATLAB and programming in general, so to better
> familiarize myself with it, I have been writing scripts for simple games
> (coin flip, dice roll, etc.) I wanted to try something more complex and
> wrote a script for a game of blackjack. It works fine except for the
> fact that I can only assign one value to aces (as opposed to two). I
> have it set up as a conditional statement:
> A=[1,11];
> if (hand-A)<=10
> A=11
> else
> A=1
> end
> I have discovered that this does not work and A comes up as 1 no matter
> what. What exactly am I doing wrong, or am i going about this the wrong
> way altogether?
> Thank you

This comes up a lot; just answered another poster's query on same issue
in Answers forum a few minutes ago.

It has to do with what the definition of 'True' is in an if (or while or
logical expression) for quantities other than single values. From the
doc for IF or WHILE one reads--

"An expression is true when its result is nonempty and contains only
nonzero elements (logical or real numeric)."

The result is that an expression is true IFF all elements of the
expression are True (nonzero) and, presuming that hand is a variable the
sum of the hand value and hence a single element, the quantity [hand-A]
is a vector and the relationship to a given single constant can never be
true.

What you're looking for is to not define A as a vector ever; only assign
either 1/11 initially and then do the test. Or, alternatively,
initialize A to zero instead and then your sum doesn't need to even
account for sum without the ace value.

--

Viewing all articles
Browse latest Browse all 41

Trending Articles