A logical for/if/while loop
2 views (last 30 days)
Show older comments
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 Comments
Jan
on 11 Jun 2018
Edited: Jan
on 11 Jun 2018
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?
Accepted Answer
Image Analyst
on 11 Jun 2018
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));
More Answers (1)
Steven Lord
on 11 Jun 2018
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 Comments
Steven Lord
on 11 Jun 2018
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.]
See Also
Categories
Find more on General Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!