How to arrange in specific order

1 view (last 30 days)
Mihir Rathod
Mihir Rathod on 21 Dec 2021
Commented: Walter Roberson on 21 Dec 2021
Good morning ,
i have one question. i want to make a function, that displays random cards and its numbers. it should display n cards and n numbers, n is the input from my side, but i want it to display it in specific order, For exp . i want first club then spades then heart and then diamond. in short the priority order should be Club>spades>heart>diamond and for numbers the priority should be A>K>D>B>Z>9>8>7. i have tried it, i was able to show the random cards with the numberbut i am having trouble displaying it in the above given order. It would be really helpfull if anyonce can help me here, Thank you in advance. Here is my code :

Answers (1)

Walter Roberson
Walter Roberson on 21 Dec 2021
Do not create the values inside a loop. Instead,
Typs = randi(4, n, 1);
Werts = randi(8, n, 1);
sorted_cards = sortrows([Typs, Werts]);
card_types = Kartentyp(sorted_cards(:,1));
card_wertes = Kartenwert(sorted_cards(:,2));
To_Display = compose("KartenTyp:%s KartenWerte:%s", card_types, card_wertes);
Walter Roberson
Walter Roberson on 21 Dec 2021
Kartentyp = {'Kreuz'; 'Pik '; 'Herz '; 'Karo '};
Kartenwerte = {'A'; 'K'; 'D'; 'B'; 'Z'; '9'; '8'; '7'};
n = 10;
nTyp = length(Kartentyp);
nWer = length(Kartenwerte);
Typs = repmat(1:nTyp, 1, nWer);
Typs = Typs(randperm(length(Typs), n));
Werts = repmat(1:nWer, 1, nTyp);
Werts = Werts(randperm(length(Werts), n));
sorted_cards = sortrows([Typs(:), Werts(:)]);
card_types = Kartentyp(sorted_cards(:,1));
card_wertes = Kartenwerte(sorted_cards(:,2));
To_Display = compose("KartenTyp:%s KartenWerte:%s", string(card_types), string(card_wertes));
KartenTyp:Pik KartenWerte:A KartenTyp:Pik KartenWerte:8 KartenTyp:Pik KartenWerte:7 KartenTyp:Herz KartenWerte:A KartenTyp:Herz KartenWerte:D KartenTyp:Herz KartenWerte:Z KartenTyp:Karo KartenWerte:A KartenTyp:Karo KartenWerte:9 KartenTyp:Karo KartenWerte:9 KartenTyp:Karo KartenWerte:8

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by