필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Help with this code.

조회 수: 1 (최근 30일)
Abhinav
Abhinav 2015년 2월 26일
마감: MATLAB Answer Bot 2021년 8월 20일
for i = 1:5
b1(i,:)= round(((1860-0)*(rand(1)))+0)
end
b1
for i = 1:5
b2(i,:)= round(((1908-0)*(rand(1)))+0);
end
b2
for i = 1:5
b3(i)= round(((1316-0)*(rand(1)))+0);
end
b3
for i = 1:5
b4(i)= round(((2940-0)*(rand(1)))+0);
end
b4
for i=1:5
b5(i,:)= round(((1860-0)*(rand(1)))+0);
end
b5
for i=1:5
b6(i,:)= round(((1860-0)*(rand(1)))+0);
end
b6
In this code I want a condition that if b5+b6=b1 then only it should execute the result otherwise search for solutions until this criterion matches. Please help me with this.
  댓글 수: 3
Abhinav
Abhinav 2015년 2월 26일
This is a supply chain problem. b1 are the set of products which are produced by manufacturer. b5 and b6 are the quantities of product sent to two different customers. Hence I have to generate b1 first.
Guillaume
Guillaume 2015년 2월 26일
편집: Guillaume 2015년 2월 26일
Then generate b6 as b1-b5?
Given two of b1, b5 or b6 there is only one of the other that work, so why generate random invalid sets for that other, when you could just calculate the unique set that would work.

답변 (1개)

Guillaume
Guillaume 2015년 2월 26일
편집: Guillaume 2015년 2월 26일
As per my comment, it seems very silly to generate random sets that have no way of working when you could just generate sets that always work, so this will get you there faster:
invalidset = true;
while invalidset
b1 = randi([0 1860], 5, 1);
%... same for b2, b3, ...
b5 = randi([0 1860], 5, 1);
b6 = b1 - b5;
invalidset = any(b6 <0 | b6 > 1860);
end
But if you really want to waste time generating a random bunch of 3 sets that have no chance of working, it's:
invalidset = true;
while invalidset
b1 = randi([0 1860], 5, 1);
%... same for b2, b3, ...
b5 = randi([0 1860], 5, 1);
b6 = randi([0 1860], 5, 1);
invalidset = ~isequal(b1, b5 + b6);
end
First option took 0.007044 seconds to generate a set on my machine. Second option is still running after a minute.

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by