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!