Random number with no repeats in set matrix

조회 수: 11 (최근 30일)
Rainaire Hansford
Rainaire Hansford 2019년 4월 14일
댓글: Walter Roberson 2019년 6월 28일
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월 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일
Old trick that randperm() used to implement:
[~, S] = sort(rand(k, p), 2);
  댓글 수: 14
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일
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일
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
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Link-Level Simulation에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by