Random number with no repeats in set matrix

Hello again.
I want to make a matrix with random numbers that do not repeat in each row in a k X p matrix
and can be modified depending on inputs.
So far i have a code:
clc
k=input('# rows ');
p=input('# columns')
S=randperm(p);
S=S(1:(k*p));
S=reshape(S,k,p)
This works some times but not all the times.
Please help?

4 件のコメント

Walter Roberson
Walter Roberson 2019 年 4 月 14 日
1:(k*p) is going to involve values greater than p unless k is 0 or 1, so S(1:(k*p)) would have index out of range.
Rainaire Hansford
Rainaire Hansford 2019 年 4 月 19 日
Ok so I decided to do this:
clc
k=input('# of rows ')
s=randi(100,[k,10])
Now I know numbers will repeat.
Is there a way to make a code detect repeated numbers?
Walter Roberson
Walter Roberson 2019 年 4 月 20 日
編集済み: Walter Roberson 2019 年 4 月 20 日
hasdup = any( sum(bsxfun(@eq, s, permute(unique(s), [2 3 1])),2) > 1, 3);
Now hasdup is true for the rows of s which contain duplicate values within the row (and so you will need to regenerate the row.)
It is not clear to me why you do not use the sort-based code that I posted: it is guaranteed that the rows of S will contain unique integers in the range 1 to p with no need to regenerate.
Rainaire Hansford
Rainaire Hansford 2019 年 4 月 20 日
it is because i wanted to change the range any time so far it only give me number 1 to 5 at random.
How do i get it to give me number from 1 to 100 but I want a k by p matrix?

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

 採用された回答

Walter Roberson
Walter Roberson 2019 年 4 月 14 日

1 投票

Old trick that randperm() used to implement:
[~, S] = sort(rand(k, p), 2);

14 件のコメント

Rainaire Hansford
Rainaire Hansford 2019 年 4 月 20 日
How do i change the range of numbers
Walter Roberson
Walter Roberson 2019 年 4 月 20 日
編集済み: Walter Roberson 2019 年 4 月 20 日
[~, Stemp] = sort(rand(k, maximum_of_range), 2);
S = Stemp(:, 1:p) ;
No need for any rejection and retry.
Rainaire Hansford
Rainaire Hansford 2019 年 4 月 23 日
Walter your awesome as always.
But now i got challege for you if I created this code like so:
clc
k=input('# of rows ')
w=1;
s=input('Max Range ');
p=input('# of Cells');
%password
[~, Stemp] = sort(rand(w, s), 2);
winner = Stemp(:, 1:p)
%matching number
[~, Stemp] = sort(rand(k, s), 2);
S = Stemp(:, 1:p)
cost=k*2
Is there a way for the program to stop running when the two sets of number (%password and %matching numbers) have the same number? The number do not need to be in the same order just have the same numbers.
Walter Roberson
Walter Roberson 2019 年 4 月 23 日
any( ismember(sort(winner,2), sort(S,2), 'rows') )
You could exchange S and winner in that code.
Rainaire Hansford
Rainaire Hansford 2019 年 4 月 25 日
Awesome so i used it but the end result:
ans =
logical
0
i am assume that means that none match right?
Walter Roberson
Walter Roberson 2019 年 4 月 25 日
Right.
Note these are for exact wins.
Rainaire Hansford
Rainaire Hansford 2019 年 4 月 26 日
編集済み: Rainaire Hansford 2019 年 4 月 26 日
Oh so does that mean it has to be in the same order?
Is there a way to make the code know that there are some number matching in any order
Walter Roberson
Walter Roberson 2019 年 4 月 26 日
The sort() calls already take care of matching in any order.
By exact wins I mean that all "p" (e.g., 6) of the numbers need to match (in any order), a total "jackpot". It does not attempt to find, for example, that a match of 3 out of 6 numbers on a "ticket".
Rainaire Hansford
Rainaire Hansford 2019 年 5 月 22 日
Oh ok so I can have it find a match with just 5 number or less it needs to be all or none?
Rainaire Hansford
Rainaire Hansford 2019 年 5 月 22 日
Can you explain
any( ismember(sort(S,2), sort(winner,2), 'rows') )
a bit more. Like the what does number 2 stand for?
Walter Roberson
Walter Roberson 2019 年 5 月 22 日
The 2 means second dimension -- sort along rows. The player's numbers are in S, one set per row, and are not assumed to be in sorted order. The winning numbers are in winner, one set per row, and are not assumed to be in sorted order. The sort(S,2) sorts each row of the player numbers independently, and the sort(winner,2) sorts each row of the winning numbers independently, and then the ismember() with 'rows' looks for exact matches of (sorted) player number rows against (sorted) winning number rows.
"Oh ok so I can have it find a match with just 5 number or less it needs to be all or none?"
You would use different code, that's all.
Rainaire Hansford
Rainaire Hansford 2019 年 5 月 23 日
Ok so I got this code it randomize the order between 1 and 5.
clc
k=1;
s=5;
w=1;
p=5;
S=randperm(s);
S=S(1:(k*5));
S=reshape(S,k,5)
[~, Stemp] = sort(rand(w, s), 2);
winner = Stemp(:, 1:p)
any( ismember(sort(S,2), sort(winner,2), 'rows') )
When I run it I get:
S =
2 1 5 4 3
cost = 2
winner =
2 1 5 3 4
ans = 1
Does "ans=1" mean that all of them match or only one of them match?
If it means all of them match is there a way to see if less then all of them match and is there a way to make the code check all rows?
Rainaire Hansford
Rainaire Hansford 2019 年 5 月 23 日
Also this code will not allow me to run mutliple rows for S. When I change "k" to 2 or more it will give a error saying Im out of bound or something like so.
Rainaire Hansford
Rainaire Hansford 2019 年 6 月 4 日
What can I do to make my code find any amount of matching numbers and not all of them?
Please provide example. Thank you

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

その他の回答 (2 件)

Steven Lord
Steven Lord 2019 年 6 月 21 日

1 投票

You have a requirement that no row may contain the same number twice. Can different rows in the same matrix contain the same number? So in the example below, A would not be acceptable (the second row has two 3's) but B would be acceptable (the first and second rows each have 2)?
A = [1 2; 3 3];
B = [1 2; 2 3];
If so, call randperm once for each row, telling it to generate size(B, 2) numbers from 1 to your upper limit.
nrows = 10;
ncols = 7;
maxvalue = 100;
A = zeros(nrows, ncols);
for whichrow = 1:nrows
A(whichrow, :) = randperm(maxvalue, ncols);
end
If you call rng default immediately before calling that code the resulting matrix should have three rows that contain the number 4 and three rows that contain 89, but that neither 4 nor 89 repeat in the same row.
Rainaire Hansford
Rainaire Hansford 2019 年 6 月 21 日

0 投票

What can I do to make my code find any amount of matching numbers and not all of them?
Please provide example. Thank you

7 件のコメント

Walter Roberson
Walter Roberson 2019 年 6 月 21 日
Proceed row by row. At each row, ismember() to find the matching numbers, and sum() or nnz() to get the count.
Rainaire Hansford
Rainaire Hansford 2019 年 6 月 25 日
Did you mean column because I thought my code was ismember() each row of S?
Walter Roberson
Walter Roberson 2019 年 6 月 25 日
k = 1;
s = 5;
w = 1;
p = 5;
S = randperm(s);
S = S(1:(k*5));
S = reshape(S,k,5);
[~, Stemp] = sort(rand(w, s), 2);
winner = Stemp(:, 1:p);
num_match = zeros(k, w);
for row = 1 : k
for winset = 1 : w
num_match(k, winset) = nnz( ismember(S(k,:), winner(winset,:)) );
end
end
This will have one row for each player entry, and one column for each winning number set, with the values being the number of matches of that player entry against that winning number set.
This assumes that there might be more than one set of winning numbers (your w parameter) and that the player might have entered multiple times (your k parameter.)
You can proceed from this to filter out such as num_match >= 3 to find 3 or more matches.
Rainaire Hansford
Rainaire Hansford 2019 年 6 月 28 日
Your the best Walter thank you
Walter Roberson
Walter Roberson 2019 年 6 月 28 日
By the way, there are vectorized solutions that can work when the amount of data is not big.
Rainaire Hansford
Rainaire Hansford 2019 年 6 月 28 日
What do you mean?
Walter Roberson
Walter Roberson 2019 年 6 月 28 日
You can use reshape and bsxfun to compare each member of one array to each member of another, resulting in a k by s by w by p array. You then any() along one of the dimensions and sum along a different dimension and squeeze to get a 2d array of results.

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

カテゴリ

製品

リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by