If statement with many logical or.

This is my code. I have a1 = 0.4 and b1 =0.2 and 1-a1-b1 = 0.4. Middle condition of if statement is satisfied but still it doesn't work. please help.
% code
for a1 = 0.4 : 0.1 : 1
for b1 = 0.2 : 0.1 : 1
if (a1 == b1 && b1 > 1-a1-b1) || (1-a1-b1 == a1 && a1 > b1) || (1-a1-b1 == b1 && b1 > a1)
continue;
end
file_name = sprintf('file_%.2f_%.2f_%.2f.txt',a1,b1,1-a1-b1);
fileID = fopen(file_name, 'w+');
fclose(fileID);
end
end

9 comentarios

if didt meet the middle condition, i guess you want to break the for loop (for b1=0.2:0.1:1).
% code
for a1 = 0.4 : 0.1 : 1
for b1 = 0.2 : 0.1 : 1
if (a1 == b1 && b1 > 1-a1-b1) || (1-a1-b1 == a1 && a1 > b1) || (1-a1-b1 == b1 && b1 > a1)
continue;
else
break;
end
file_name = sprintf('file_%.2f_%.2f_%.2f.txt',a1,b1,1-a1-b1);
fileID = fopen(file_name, 'w+');
fclose(fileID);
end
end
Does it work for you?
sourabh mittal
sourabh mittal el 8 de Oct. de 2018
Editada: sourabh mittal el 8 de Oct. de 2018
No, it doesn't work. i want to skip for that particular value of a1 and b1, and go for next iteration if conditions of 'if' statement are satisfied.
How about this?
% code
for a1 = 0.4 : 0.1 : 1
for b1 = 0.2 : 0.1 : 1
if (a1 == b1 && b1 > 1-a1-b1) || (1-a1-b1 == a1 && a1 > b1) || (1-a1-b1 == b1 && b1 > a1)
break;
end
file_name = sprintf('file_%.2f_%.2f_%.2f.txt',a1,b1,1-a1-b1);
fileID = fopen(file_name, 'w+');
fclose(fileID);
end
end
madhan ravi
madhan ravi el 8 de Oct. de 2018
Editada: madhan ravi el 8 de Oct. de 2018
Can you explain what you want to obtain ?
Adam
Adam el 8 de Oct. de 2018
Editada: Adam el 8 de Oct. de 2018
Which next iteration do you want to go to? If you want to go to the next iteration of the inner loop than 'continue' works fine if your logical statement is correct to catch the case you want. If you want to skip to the next iteration of the outer loop then 'break' is required.
So what is the actual problem you are having? Is the logical statement correct, but the behaviour when it matches that is not what you want or is it not continuing when you expect because the logical statement is not correct?
sourabh mittal
sourabh mittal el 8 de Oct. de 2018
no it still doesn't work. I guess problem is in comparing flot values.
sourabh mittal
sourabh mittal el 8 de Oct. de 2018
I want to to go to the next iteration of the inner loop, so 'continue' works fine. but when a1 = 0.4 and b1 =0.2 , (middle condition of if is satisfied) i am still getting a file_0.4_0.2_0.4.
Adam
Adam el 8 de Oct. de 2018
Yes, looking at the loop again, this could well cause a problem.
% code
for a1 = 0.4 : 0.1 : 1
for b1 = 0.2 : 0.1 : 1
if (1-a1-b1 == a1 && a1 > b1)
continue;
end
file_name = sprintf('file_%.2f_%.2f_%.2f.txt',a1,b1,1-a1-b1);
fileID = fopen(file_name, 'w+');
fclose(fileID);
end
end
See this condition of if statement. this is not working as i described above.

Iniciar sesión para comentar.

 Respuesta aceptada

Dennis
Dennis el 8 de Oct. de 2018
You are comparing floating point numbers, which will not necessary work:
1-0.4-0.2==0.4
ans =
0
You can use
abs(1-a1-b1-a1)<0.001
For more information please look here.

Más respuestas (2)

John D'Errico
John D'Errico el 8 de Oct. de 2018
Your problem is EXACTLY in that you are trying to compare floating point numbers, for exact equality. This is something you should never do, unless you completely understand floating point numbers, and how to know when you can do it safely. And even then you still need to take extra care.
But tests like this:
b1 > 1-a1-b1
are just a flat out bad idea. NEVER DO THAT. NEVER. Must I say it again? Don't do it. Period. For example, just how does 0.4 compare to 1-0.2-0.4? Surely they are the same number?
sprintf('%0.55f',0.4)
ans =
'0.4000000000000000222044604925031308084726333618164062500'
sprintf('%0.55f',1 - 0.4 - 0.2)
ans =
'0.3999999999999999666933092612453037872910499572753906250'
Not so.
Instead, you do have options.
for a1i = 4 : 10
a1 = a1i/10;
for b1i = 2 : 10
b1 = b1i/10;
if (a1i == b1i && b1i > 10-a1i-b1i) || (10-a1i-b1i == a1i && a1i > b1i) || (10-a1i-b1i == b1i && b1i > a1i)
yada yada yada...
As you should see, I changed all of your numbers to be integers, double precision integers, but still integers. All of the tests (assuming I did not mistype anything in that last line) work on INTEGERS, thus are comparisons between integers.
Compute a1 and b1 separately. Use them in floating point form, but NEVER use them for comparisons. NEVER. Well, again, unless you seriously know what you are doing, and you know why you should never use tests of that form.
ANKUR KUMAR
ANKUR KUMAR el 8 de Oct. de 2018
Write statement inside of the loop
for a1 = 0.4 : 0.1 : 1
for b1 = 0.2 : 0.1 : 1
if (a1 == b1 && b1 > 1-a1-b1) || (1-a1-b1 == a1 && a1 > b1) || (1-a1-b1 == b1 && b1 > a1)
file_name = sprintf('file_%.2f_%.2f_%.2f.txt',a1,b1,1-a1-b1);
fileID = fopen(file_name, 'w+');
fclose(fileID);
end
end
end

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 8 de Oct. de 2018

Respondida:

el 8 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by