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? :(
采纳的回答
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?
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
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.
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)
