MATLAB Answers

Find repeating values of the same numbers

2 views (last 30 days)
Pete Brimble
Pete Brimble on 7 Apr 2020
Commented: Star Strider on 7 Apr 2020
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.

  0 Comments

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 7 Apr 2020
Edited: Star Strider on 7 Apr 2020
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.
.

  2 Comments

Star Strider
Star Strider on 7 Apr 2020
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

Sign in to answer this question.