Creating an array of unique random points
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi all, I would like to create a 2D array which gives an X amount of unique random X and Y coordinates. I have no problem setting up the array but I am having trouble finding a way to detect and change duplicate values should they arise. Any help on finding a code that will find any duplicate values and change them to unique values would be appreciated, cheers.
댓글 수: 0
답변 (2개)
Walter Roberson
2018년 9월 7일
unique() with 'rows' and 'stable'. Then check the size of the result, compare to the desired size to determine how many more entries you need. Random that many more, append to the end, go through the unique and size check...
I have posted the code a few times.
댓글 수: 3
Walter Roberson
2018년 9월 7일
Sorry I do not see it at the moment. I poked around im my old posts but I did not seem to find the right keywords.
Walter Roberson
2018년 9월 7일
gives an example of creating unique random permutations, showing a loop that keeps generating until it has enough unique rows.
Image Analyst
2018년 9월 8일
Why would there be any duplicate values if you're using floating point values like you'd get from rand().
I don't understand why you're using X for both the number of points, and the x coordinate. Let's call X N and then you can get random x and y coordinates like this
x = xMin + (xMax-xMin) * rand(N, 1);
y = yMin + (yMax-yMin) * rand(N, 1);
There will be no duplicated coordinates.
You would only have duplicates (possibly) if you restricted your x and y coordinates to integers.
댓글 수: 1
Walter Roberson
2018년 9월 8일
"There will be no duplicated coordinates."
Well, probably. But an RNG that cannot produce duplicates has a limited period.
"You would only have duplicates (possibly) if you restricted your x and y coordinates to integers."
rand() * 2^53 is certain to be an integer, but has the same duplicate properties as rand() has. So it is not integers that are the factors, but rather how many bits of output you are effectively using. If you are using N bits out of the 53, then you are certain to have a duplicate by 2^N entries, and probably by very much less ("birthday paradox")
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!