selecting the random element by having distance to others

조회 수: 1 (최근 30일)
NA
NA 2020년 4월 28일
답변: Ameer Hamza 2020년 4월 29일
This code generates random number
index = randperm(length(1:200),5);
I want to control this generator. Each number have a distance to each others
for example
index=[117 121 15 14 187]
117 and 121 is near to each other also 14 amd 15. This example is not acceptable.
200 divided by 5 equals 40.
First element should be between 1 to 40
Second elemnet should be between 41 to 80
Third elemnet should be between 81 to 120
4th ---> 121 to 160
5th ---> 161 to 200
If the fist element is 39 the second one should not be 42. 39 and 42 is close to each other.
something like this result
index=[2 53 90 140 199]
  댓글 수: 1
David Hill
David Hill 2020년 4월 28일
I have no idea what you are talking about by reading your example. Please explain further.

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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 29일
If you want your random numbers to be distributed like this, the randperm is not the correct function. You can use randi() and shift them to your required range.
x = randi([1 40], 1, 5) + (0:40:160);
Result
>> x
x =
27 42 114 158 188

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by