generating random numbers within a certain range & spacing
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
if true
max=[6 7.75 8.75 9.25 9.50];
min=[5 6 7.75 8.75 9.25];
b=size(max,2);
n=20;
for i=1:b
c(i)=max(i)-min(i);
x=sort((min(i))+rand(n,1)*(c(i)), 'ascend')
y=(linspace(min(i),max(i),n))'
end
end
i want to create 20 random numbers for each c(i), but all the 20 numbers generated for each c(i) should maintain at least .025 gap among themselves. how do i do this? i cant do this with rand or linspace. anyone to help? :(
채택된 답변
Fangjun Jiang
2018년 7월 20일
randi([200 240],[20,1])/4
댓글 수: 11
ahh thats straight forward, thanks for ur answer, but cant implement with my actual code, edited. can u check it again?
Fangjun Jiang
2018년 7월 21일
편집: Fangjun Jiang
2018년 7월 21일
max() and min() are function names. Don't use them as variable names.
UpLim=[6 7.75 8.75 9.25 9.50];
LowLim=[5 6 7.75 8.75 9.25];
b=size(UpLim,2);
n=20;
x=zeros(b,n);
for i=1:b
Range=[LowLim(i), UpLim(i)];
x(i,:)=randi(400*Range,[1,n])/400;
end
Afrasiab Himaloy
2018년 7월 21일
편집: Afrasiab Himaloy
2018년 7월 21일
Thank you sir, but still the numbers are not spaced at least .025 with each other while i examined them in ascending order. these are the output: x =
5.0175 6.0200 7.8125 8.8275 9.2525
5.1150 6.1050 7.8200 8.8500 9.2625
5.2450 6.1475 7.9400 8.9225 9.2675
5.3250 6.1650 8.0075 8.9625 9.2775
5.3250 6.2350 8.0800 8.9650 9.3075
5.3425 6.2550 8.1450 8.9775 9.3400
5.3750 6.2850 8.1800 9.0025 9.3475
5.3950 6.5275 8.1850 9.0075 9.3475
5.3975 6.5675 8.2425 9.0150 9.3575
5.4375 6.7000 8.2650 9.0275 9.3625
5.4375 6.9025 8.3025 9.0300 9.3650
5.4575 6.9450 8.3425 9.0575 9.3975
5.5475 7.0000 8.3650 9.0600 9.4250
5.5625 7.1050 8.5700 9.0975 9.4275
5.5750 7.1500 8.5775 9.0975 9.4350
5.6750 7.2650 8.6375 9.1100 9.4450
5.7150 7.4575 8.6375 9.1175 9.4475
5.7225 7.5050 8.6500 9.1600 9.4600
5.8150 7.6650 8.6825 9.1700 9.4675
5.8850 7.7050 8.7475 9.2350 9.4875
see, 5.3250 twice there; 5.3750, 5.3950 & 5.3975 are not .025 distance apart. but i want all of them .025 apart, there should be no chance of repeating same number also. how do i do this, sir?
Should be 40 instead of 400 above. Then the difference should be 0.025 or more. There is still question though. If you want 20 numbers between 9.25 and 9.50, and the value needs to be 0.025 apart, then you have to have some duplicated numbers. Is it okay as long as there is no consecutive duplicated numbers?
sir 40 is not working. again, if there is no space for 20 numbers to fit between 9.25 & 9.50; output will be shown 'not possible or NaN'. how can i solve this sir?
UpLim=[6 7.75 8.75 9.25 9.50];
LowLim=[5 6 7.75 8.75 9.25];
b=size(UpLim,2);
n=20;
x=zeros(n,b);
for i=1:b
AllNum=LowLim(i):0.025:UpLim(i);
N=length(AllNum);
if N<n
disp('Not possible to fit in 20 numbers');
continue;
end
index=randperm(N,n);
x(:,i)=AllNum(index);
end
Sir, thank you for your patience. It is working good now. I appreciate your help very much.
Okay, please accept the answer as a valid solution. The question and answer got a couple of twists along the way.
Afrasiab Himaloy
2018년 7월 23일
편집: Afrasiab Himaloy
2018년 7월 23일
Sir, I do have couple of more queries, would you be kind enough to help me to solve? actually im looking for an output text file which is attached in. can you please edit the code for this output?
if true
UpLim=[6 7.75 8.75 9.25 9.50];
LowLim=[5 6 7.75 8.75 9.25];
time=[0600 0615 0630 0645 0700];
b=size(UpLim,2);
n=20;
x=zeros(n,b);
for i=1:b
AllNum=LowLim(i):0.025:UpLim(i);
N=length(AllNum);
if N<n
disp('Not possible to fit in 20 numbers');
continue;
end
index=randperm(N,n);
x(:,i)=sort(AllNum(index),'ascend');
end
end
Please formulate your problem and ask a separate question. Keep in mind that this forum is for questions and answers, not for substitution of work.
sorry sir if i am bothering a lot, but this would be helpful for this query. i will ask new questions related other problems. but this is the last one sir. because im looking for the output. sir, help me please.
추가 답변 (0개)
카테고리
도움말 센터 및 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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
