How to arrange in specific order

2 次查看(过去 30 天)
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 :

回答(1 个)

Walter Roberson
Walter Roberson 2021-12-21
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);
disp(char(cellstr(To_Display)))
  2 个评论
Walter Roberson
Walter Roberson 2021-12-21
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));
disp(char(cellstr(To_Display)))
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by