How do I check if an entire array matches the elements of another array (different size)?

13 ビュー (過去 30 日間)
Lélia van der Linden
Lélia van der Linden 2020 年 10 月 8 日
編集済み: Stephen23 2020 年 10 月 10 日
Hi!
I'm making a bingo game in which I want to display the name of the winner in a Edit Field.
I have already saved the card elements (numbers) for every person participating in the game as public variables. For example:
person1 = ([3,10,12,27,31,37,46,48,56,63,66,71,79,82,86]);
person2 = ([1,3,13,22,34,38,41,42,55,61,67,72,78,81,85]);
etc..
I'm using randperm function to call 90 numbers and storing it as in: app.num = randperm(90);
In the game, someone wins if all the numers of the array are called in sequence from the randperm function.
How can I do this by storing the sequence of the randperm function numbers in an array everytime a button is pushed and call the winner whenever someone wins?
  2 件のコメント
Stephen23
Stephen23 2020 年 10 月 9 日
編集済み: Stephen23 2020 年 10 月 10 日
"I have already saved the card elements (numbers) for every person participating in the game as public variables. For example:"
person1 = ([3,10,12,27,31,37,46,48,56,63,66,71,79,82,86]);
person2 = ([1,3,13,22,34,38,41,42,55,61,67,72,78,81,85]);
etc..
Numbering variables like that is a sign that you are doing something wrong. Putting meta-data (e.g. pseudo-indices or "their real names") into variable names is a sign that you are doing something wrong. Trying to access those individual variables will force you into writing slow, complex, buggy code that is difficult to debug:
The simple, efficient, recommended approach is to use indexing into one array (which could be a container array, e.g. a cell array, a table, or a structure).
Lélia van der Linden
Lélia van der Linden 2020 年 10 月 9 日
That was actually an example...
I'm using their real names as variable names.
But thanks anyway! Learned something new.

サインインしてコメントする。

回答 (1 件)

Mohammad Sami
Mohammad Sami 2020 年 10 月 9 日
編集済み: Mohammad Sami 2020 年 10 月 9 日
What you need is the ismember function.
if true
[Lia,Locb] = ismember(person1, person2);
Allperson1inperson2 = all(Lia);
end
This check that values in person1 exist in person2

カテゴリ

Help Center および File ExchangeCard games についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by