Assigning an iterative variable a script

4 Ansichten (letzte 30 Tage)
Tim Keegan
Tim Keegan am 5 Mai 2022
Kommentiert: Tim Keegan am 6 Mai 2022
Does anyone know how I can assign an iterative variable in a for loop a script value. I am trying to make a poker game in MatLab and when I try to assign suits in a for loop it doesn't work and it instead assigns them NAN. I know I could just make my suit vector a collection of 1,2,3,and 4 and then later assign those values to the suit name, but is there any way i could do it directly in the for loop? The following code is where the issue is (in the for loop, but I included the other code, so it would be simpler to understand). (I know spaids is spelled spades I need to go through and fix it)
deck=[2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 10 11 11 11 11 12 12 12 12 13 13 13 13 14 14 14 14];
players=input('How many players would you like to play with (max number of players is 5) ');
while players>5 | players<1 | round(players)~=players
players=input('ERROR INPUT AN INTEGER BETWEEN 1 and 5. How many players would you like to play with (max number of players is 5) ');
end
% initializing variables that will be used later
n=0;
folded=zeros(length(players));
order=randperm(52);
suit1=zeros(length(players));
suit2=zeros(length(players));
card1=zeros(length(players));
card2=zeros(length(players));
% creating the vector for the suits of the cards
suits=["spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds"];
% assigning individual players cards
for i=1:players
n=n+1;
card1(i)=deck(order(i));
card2(i)=deck(order(i+players));
suit1(i)=suits(order(n));
suit2(i)=suits(order(n+players));
fprintf('Player %1.0f your cards are %1.0f %s and %1.0f %s\n',i,card1(i),suit1(i),card2(i),suit2(i))
end
  4 Kommentare
Voss
Voss am 5 Mai 2022
Bearbeitet: Voss am 5 Mai 2022
"for some reason it wasn't messing up"
That's because, if you assign to an element of an array that's off the end of the array, MATLAB will extend the array automatically (or if the array doesn't exist yet, MATLAB will allocate it to the size required):
% card1 doesn't exist yet, so MATLAB creates it and
% gives it 5 elements, with the first 4 being 0
card1(5) = 7
card1 = 1×5
0 0 0 0 7
or similarly for a string array:
suit1(3) = "spade"
suit1 = 1×3 string array
<missing> <missing> "spade"
so, pre-allocation is not strictly necessary, but it's a good idea. When you were allocating your variables to be of size 1 and then assigning elements 2, 3, etc., it's no problem, just not optimal and not what was apparently intended.
Similarly, the other things I pointed out are not problems per se, just pointers to simplify or clarify the code.
Two other things you may find useful: repelem and repmat.
deck = repelem(2:14,1,4)
deck = 1×52
2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9
suits = repmat(["spaids","hearts","clubs","diamonds"],1,13)
suits = 1×52 string array
"spaids" "hearts" "clubs" "diamonds" "spaids" "hearts" "clubs" "diamonds" "spaids" "hearts" "clubs" "diamonds" "spaids" "hearts" "clubs" "diamonds" "spaids" "hearts" "clubs" "diamonds" "spaids" "hearts" "clubs" "diamonds" "spaids" "hearts" "clubs" "diamonds" "spaids" "hearts" "clubs" "diamonds" "spaids" "hearts" "clubs" "diamonds" "spaids" "hearts" "clubs" "diamonds" "spaids" "hearts" "clubs" "diamonds" "spaids" "hearts" "clubs" "diamonds" "spaids" "hearts" "clubs" "diamonds"
Tim Keegan
Tim Keegan am 6 Mai 2022
thank you for the help

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jon
Jon am 5 Mai 2022
Bearbeitet: Jon am 5 Mai 2022
The problem is that you preallocated suit1, and suit2 as a vector of doubles (numeric values), but then in the loop you assign strings to the individual elements
  2 Kommentare
Jon
Jon am 5 Mai 2022
Bearbeitet: Jon am 5 Mai 2022
You should instead preallocate using strings, as for example
suit1=strings(length(players),1);
Note, I think your original code also has errors preallocating suit1, suit2,card1 and card2 where you set them equal to zeros(length(players)). This will create length(players) by length(players) matrix of zeros. I think you just wanted a vector. So for a column vector preallocate card1 and card 2 using zeros(length(players),1), and for the suits use strings(length(players),1) as shown above
Tim Keegan
Tim Keegan am 5 Mai 2022
Thank you this worked. I had tried doing it without preallocating at all and it hadnt worked, but this worked.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by