I want to make random values that satisfies specific conditions. But I have a problem. help me please.

1 次查看(过去 30 天)
I want to make random values that satisfies (a+b<2) and (a+b>2).The range of 'a' is -1<a<0 and the range of 'b' is 2<b<3. But I have a problem. help me please. I tried to make random value of 'a' and 'b' and then find the combination of numbers but I can`t.
Below is the algorithm i made.
a = -1 + (1+0)*rand(10000,1);
b = 2 + (1+0)*rand(10000,1);
for i = 1:10000
for j = 1:10000
if a(i,1) + b(j,1) < 0
c(i,1) = a(i,1);
c(i,2) = b(j,2);
end
end
end

采纳的回答

Stephan
Stephan 2018-4-23
编辑:Stephan 2018-4-23
Hi,
you generate numbers:
-1 < a < 0 and 2 < b < 3
your selection criterion for c is:
if a(i,1) + b(j,1) < 0
No combination of a+b will ever be < 0. All possible combinations of a+b will always be between 1 < (a+b) < 3.
That is why you get no values for c.
Change the condition in a way, that is satisfiable, then your code will run.
Here you have an example with the condition a+b < 2 (see comments for further information, there was an indexing error at line 9 also):
a = -1 + (1+0)*rand(10,1); % becareful with number of values
b = 2 + (1+0)*rand(10,1); % when you plan to combine them
d = 0; % counter for matrix c, its unknown yet how many values it gets
for i = 1:10
for j = 1:10
if a(i,1) + b(j,1) < 2
d = d+1;
c(d,1) = a(i,1);
c(d,2) = b(j,1); %before: b(j,2) --> b has only 1 column
end
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 난수 생성 的更多信息

Community Treasure Hunt

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

Start Hunting!