A logical for/if/while loop

11 次查看(过去 30 天)
The problem can be formulated as the following: there is a line carrying a fluid, at the end the line splits into two (like a fork in the road). The fluids properties are read by two independent sensors at the two prong ends of the fork. This gives us two time dependent vectors about the fluids properties, well call these vectors prop_fork_1 and prop_fork_2. Furthermore (this is where the problem is), each fork prong has a valve that is either open (1), or closed (0). This now gives us another two time dependent vectors of values of either one or zero, well call those valve_fork_1 and valve_fork_2. The goal is to create a new (time dependent) vector called prop_true that takes the appropriate value of the fluids property depending on whether the forks valves are open or closed. So for example, if in a given time step valve_fork_1 reads 1 (open) while valve_fork_2 reads 0 (closed) then the appropriate value for that time step would be cell value from vector prop_fork_1 (and vise versa). If both valves are open in a given time step then the value for prop_true would be (prop_fork_1+prop_fork_2)/2. If neither are open then the appropriate value for prop_true would be zero.
I have attempted an if, while loop (with some for's too) but am having issues. For one, the loop runs indefinitely. I believe that this code is close an would be able to be fixed up by the right set of eyes. Thank you in advance and I hope you enjoy the logic problem!
if true
% code
end
valve_fork_1=[0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,0,0,0,1,1,1,1,1]'; %example vectors
valve_fork_2=[0,0,1,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1]';
prop_fork_1=ones(length(valve_fork_1),1).*300;
prop_fork_2=ones(length(valve_fork_1),1).*500; %example vectors for our purpse
prop_fork_1=valve_fork_1.*prop_fork_1;
prop_fork_2=valve_fork_2.*prop_fork_2; %this creates vectors that have either 0 or a value from the prop sensor
%
prop_true=zeros(length(valve_fork_1),1);
for i=1:1:length(valve_fork_1)'
if logical(prop_fork_1(i))==1
while logical(prop_fork_2(i))==0
prop_true(i)=prop_fork_1(i);
if logical(prop_fork_2(i))==1
while logical(prop_fork_1(i))==0
prop_true(i)=prop_fork_2(i);
if logical(prop_fork_1(i))==1
while logical(prop_fork_2(i))==1
prop_true(i)=(prop_fork_1(i)+prop_fork_2(i))/2;
end
end
end
end
end
end
end
  2 个评论
Jan
Jan 2018-6-11
编辑:Jan 2018-6-11
You have posted the text of the question formatted as code and the code formatted as text. Now both is hard to read. Please read http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup .
Did you mention, which problems you have?
Eli
Eli 2018-6-11
yes I noticed that immediately and fixed it. Thanks

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2018-6-11
Way, way too complicated. Simply sum the values and use that to extract or assign the appropriate props numbers:
valve_fork_1 = [0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,0,0,0,1,1,1,1,1]'; %example vectors
valve_fork_2 = [0,0,1,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1]';
prop_fork_1 = valve_fork_1.*300;
prop_fork_2 = valve_fork_2.*500; %example vectors for our purpse
prop_fork_1 = valve_fork_1.*prop_fork_1;
prop_fork_2 = valve_fork_2.*prop_fork_2; %this creates vectors that have either 0 or a value from the prop sensor
sum_valve = valve_fork_1 + 2 * valve_fork_2;
% only1 = sum_valve == 1; % Not really needed actually
only2 = sum_valve == 2;
both = sum_valve == 3;
prop_true = prop_fork_1; % Initialize
prop_true(only2) = prop_fork_2(only2);
prop_true(both) = 0.5 * (prop_fork_1(both) + prop_fork_2(both));
  1 个评论
Eli
Eli 2018-6-11
编辑:Eli 2018-6-11
this guy is a genius. Can I hire you as a life coach for other over complexities? Thank you very much!

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2018-6-11
Your outermost while loop has condition:
while prop_fork_2(i)==0
Where inside the body of the first while loop do you change prop_fork_2? If the condition is satisfied when you first reach the while keyword, will it ever not be satisfied?
You did define prop_fork_2 initially as the product of two variables, but changing either of those two variables after that initial definition will not "automatically recompute prop_fork_2" or anything like that. If you want it to be recomputed, you must recompute it inside the while loop.
  2 个评论
Eli
Eli 2018-6-11
Steven,
thank you for your response. I did not recompute prop_fork_2 because I want to create a new vector called true_prop that has a mixture of values from prop_fork_2 and prop_fork_1. The exact value in the ith cell of true_prop is to be determined by the conditions in the loop. e.g. for a given ith value, if prop_fork_1 has a nonzero value and prop_fork_2 does not, then the ith value of true_prop is the nonzero value in prop_fork_1 etc..
So I did not change the value of prop_fork_2 within the loop... Is that something I still need to include?
Thanks for your help. ~Eli
Steven Lord
Steven Lord 2018-6-11
y = 0;
x = y + 1;
while x > 0
y = y - 1;
drawnow
end
When will this while loop finish? This is a greatly simplified version of your outermost while loop. Let it run for a minute or two then Ctrl-C and look at the values of x and y and you should be able to convince yourself that this will never finish. [The drawnow is to give MATLAB a chance to process your Ctrl-C to break out of the loop.]

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 General Applications 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by