How to generate 8 random 2D coordinates that do not duplicate?
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
Alan Stevens
2021년 2월 1일
How about
P = randperm(64);
[I,J] = ind2sub([8,8],P);
x = I(1:8);
y = J(1:8);
댓글 수: 3
Mohammed Al-Fadhli
2021년 2월 1일
편집: Mohammed Al-Fadhli
2021년 2월 1일
Alan Stevens
2021년 2월 1일
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
2021년 2월 1일
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!