What is wrong with my code? (Matlab)

1 view (last 30 days)
  • I have to make my code draw all 52 cards from a deck and return what I have drawn each time, so the result would be f.x.:
  • You have drawn the Ace of hearts.
  • You have drawn the 10 of spades.
  • You have drawn the 2 of spades.
  • .... and so on until every card is gone from the deck.
  • This is my code so far:
  • A=randperm(52)
  • for i=1:length(A)
  • %sort
  • switch i
  • case {1,2,3,4,5,6,7,8,9,10,11,12,13}
  • sort='hearts';
  • case {14,15,16,17,18,19,20,21,22,23,24,25,26}
  • sort='spades';
  • case {27,28,29,30,31,32,33,34,35,36,37,38,39}
  • sort='clubs';
  • case {40,41,42,43,44,45,46,47,48,49,50,51,52};
  • sort='diamonds';
  • end
  • %spil
  • if A==1;
  • spil='ace';
  • elseif A==14;
  • spil='ace';
  • elseif A==27;
  • spil='ace';
  • elseif A==40;
  • spil='ace';
  • elseif A==11;
  • spil='jack';
  • elseif A==24;
  • spil='jack';
  • elseif A==37;
  • spil='jack';
  • elseif A==50;
  • spil='jack';
  • elseif A==12;
  • spil='queen';
  • elseif A==25;
  • spil='queen';
  • elseif A==38;
  • spil='queen';
  • elseif A== 51;
  • spil='queen';
  • elseif A==13;
  • spil='king';
  • elseif A==26;
  • spil='king';
  • elseif A==39;
  • spil='king';
  • elseif A==52;
  • spil='king';
  • else spil=num2str(A);
  • end
  • disp(['You have drawn ',spil,'of ',sort])
  • end
  • But it returns everything in a bundle.
  • What am I doing wrong?

Accepted Answer

Image Analyst
Image Analyst on 26 Oct 2015
lilja, I just did this very same problem for nina here: http://www.mathworks.com/matlabcentral/answers/250975#comment_318533 Is it just a coincidence that you both are trying to solve the same problem, or it is homework? Neither one tagged it as homework. If it is homework, tag it as such, but unfortunately you can't turn in my code or else you might get in to trouble.
  2 Comments
Lilja Pálsdóttir
Lilja Pálsdóttir on 26 Oct 2015
Edited: Lilja Pálsdóttir on 26 Oct 2015
We are in the same class, this is homework but still not, kind of hard to explain, but your code helped me alot, thank you :)
Image Analyst
Image Analyst on 26 Oct 2015
Edited: Image Analyst on 26 Oct 2015
Like this:
if a >= 1 && a <= 13
disp(.....
I applied the "homework" tag for you. If my code answered your question, can you mark it as "Accepted"? Thanks in advance.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!