How to generate 8 random 2D coordinates that do not duplicate?
Show older comments
Hi!
So i wonder how to get 8 random (x,y) coordinates that won't duplicate. For example x=[1 1,1,2,8] and y=[1 2 3 2 7]. In this example there is no coordinates that repeat themselfs. But if for example x=[1,1,1,2,8] and y=[1,1,3,2,7] then the first and scound points duplicates beacuse they have same coordinates (1,1). Another condition is that the coordinates need to be integers from 1-8. Whith that said x should have a value from 1-8 and y should have a value from 1-8.
So i have tried with this code but i get duplicates :(.
clc; clear; close all;
x=[1:8];
y=[1:8];
P=[x;y];
P(randperm(16)) =P
Accepted Answer
More Answers (1)
Alan Stevens
on 1 Feb 2021
How about
P = randperm(64);
[I,J] = ind2sub([8,8],P);
x = I(1:8);
y = J(1:8);
3 Comments
Mohammed Al-Fadhli
on 1 Feb 2021
Edited: Mohammed Al-Fadhli
on 1 Feb 2021
Alan Stevens
on 1 Feb 2021
You have an 8x8 grid (8 x values and 8 y values), making 64 grid points in total. Think of them as numbered columnwise from 1 to 64. Shuffle these numbers (randperm). Then assume the shuffled numbers are rearranged into an 8x8 grid. The shuffled numbers are indices (ind) and the 8x8 grid has 2dimensional subscripts (sub). So ind2sub calculates the 2d subscripts that correspond to the 1d indices.
doc ind2sub
Mohammed Al-Fadhli
on 1 Feb 2021
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!