Assigning an iterative variable a script

조회 수: 3 (최근 30일)
Tim Keegan
Tim Keegan 2022년 5월 5일
댓글: Tim Keegan 2022년 5월 6일
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
Voss
Voss 2022년 5월 5일
편집: Voss 2022년 5월 5일
"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 2022년 5월 6일
thank you for the help

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

채택된 답변

Jon
Jon 2022년 5월 5일
편집: Jon 2022년 5월 5일
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
Jon
Jon 2022년 5월 5일
편집: Jon 2022년 5월 5일
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 2022년 5월 5일
Thank you this worked. I had tried doing it without preallocating at all and it hadnt worked, but this worked.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by