Find repeating values of the same numbers

2 次查看(过去 30 天)
Say G1=randperm(59,7) = "43 9 38 30 54 36 59", we can sort this using "sort(G1)" to make it easier for the user to read, therefore creating, "9 30 36 38 43 54 59"
Now take G2=randperm(59,7) = "41 7 30 54 29 50 59", again using "sort(G2)" to create, "7 29 30 41 50 54 59"
G1 and G2 share 3 of the same values; "30 50 and 59".
How do you display or state there are three of the same values?
After finding out whether they are value of the same i want to display the following statements to the user.
For example if 1 number matches, the user wins £10, 2x numbers = £50 and 3x numbers =£100.
I presume a "for loop" and "disp" will be required to indicate what the player uses.

回答(1 个)

Star Strider
Star Strider 2020-4-7
编辑:Star Strider 2020-4-7
One approach is to use the intersect function:
G1 = [9 30 36 38 43 54 59];
G2 = [7 29 30 41 50 54 59];
G1G2 = intersect(G1,G2)
producing:
G1G2 =
30 54 59
For the prizes:
Match = numel(G1G2);
Prize = {'£10','£50','£100'};
fprintf('You won %s!\n', Prize{Match})
so here:
You won £100!
Considering other possible results:
if Match > 0
fprintf('You won %s!\n', Prize{min(Match,3)})
end
EDIT — Added the prizes output.
.

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by