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
Fangjun Jiang 2018 年 7 月 20 日

1 投票

randi([200 240],[20,1])/4

11 件のコメント

Afrasiab Himaloy
Afrasiab Himaloy 2018 年 7 月 20 日
ahh thats straight forward, thanks for ur answer, but cant implement with my actual code, edited. can u check it again?
Fangjun Jiang
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
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?
Fangjun Jiang
Fangjun Jiang 2018 年 7 月 22 日
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?
Afrasiab Himaloy
Afrasiab Himaloy 2018 年 7 月 22 日
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?
Fangjun Jiang
Fangjun Jiang 2018 年 7 月 23 日
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
Afrasiab Himaloy
Afrasiab Himaloy 2018 年 7 月 23 日
Sir, thank you for your patience. It is working good now. I appreciate your help very much.
Fangjun Jiang
Fangjun Jiang 2018 年 7 月 23 日
Okay, please accept the answer as a valid solution. The question and answer got a couple of twists along the way.
Afrasiab Himaloy
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
Fangjun Jiang
Fangjun Jiang 2018 年 7 月 23 日
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.
Afrasiab Himaloy
Afrasiab Himaloy 2018 年 7 月 23 日
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 ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by