A logical for/if/while loop

2 visualizaciones (últimos 30 días)
Eli
Eli el 11 de Jun. de 2018
Comentada: Steven Lord el 11 de Jun. de 2018
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 comentarios
Jan
Jan el 11 de Jun. de 2018
Editada: Jan el 11 de Jun. de 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?
Eli
Eli el 11 de Jun. de 2018
yes I noticed that immediately and fixed it. Thanks

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 11 de Jun. de 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));
  1 comentario
Eli
Eli el 11 de Jun. de 2018
Editada: Eli el 11 de Jun. de 2018
this guy is a genius. Can I hire you as a life coach for other over complexities? Thank you very much!

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 11 de Jun. de 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 comentarios
Eli
Eli el 11 de Jun. de 2018
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 el 11 de Jun. de 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.]

Iniciar sesión para comentar.

Categorías

Más información sobre General Applications en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by