When using an 'if' loop, if the variable creating the condition changes already in the first condition it applies that change to go also to the second condition, why?
2 views (last 30 days)
Show older comments
for sr=1:vrl0
if method_1(1,sr)==1
if error_check_full(sr)==0
for i=1:split_number00
new_split_full(sr,i,:)=new_split_full_out(:,sr);
end
for i=1:split_number00
size_interval_1(sr,i)=(new_split_full(sr,i,2)-new_split_full(sr,i,1))+1;
end
size_interval(sr)=max(size_interval_1(sr,:));
split_number0(1,sr)=1;
method_1(1,sr)=1;
else
count_method_1=count_method_1+1;
[size_variables_m12,~]=size(error_vector_final_full_1(:,sr));
%%Method 1, sample split. (New sample split considering the error values)
%% 1.Save limits
%Input: size_variables_m1 && error_vector from the previous loop
ERROR_LIMIT=ERROR_LIMIT0;
ac=length(find(error_vector_final_full_1(:,sr)<(-ERROR_LIMIT)));
cc=length(find(error_vector_final_full_1(:,sr)>ERROR_LIMIT));
if all_count==0
(...)
if all_count==0
method_1(1,sr)=1;
else
method_1(1,sr)=0;
end
else
THEN IT APPLIES THE FIRST PART OF THE IF AND THE SECOND ONE AFTER THE ELSE BECAUSE method_1 changes inside the first condition.
There is no doubt I can solve it by flipping variables or introducing a new one but what is the purpose of that if that could have been achieved by making two loops or with a while loop one?, should not the conditions variables apply just with the initial value when entering into the loop?
Thank you in advance! Either I need to understand the purpose of that development for the if function or it must be improved?
0 Comments
Answers (1)
dpb
on 23 Jan 2023
Moved: dpb
on 23 Jan 2023
Conditions are evaluated based on the values of the variables at the time the condition is tested -- so, if you change the variable and then do a subsequent test on that same variable, then it seems perfectly obvious the result at the time of the subsequent test would be the new result. Otherwise, to turn your Q? around, what's the point of changing the variable if it wouldn't make any difference that it changed in a subsequent test.
You have a logic issue in the code. It certainly isn't at all clear what the end result/purpose of this code is intended to be, but the problem is not the behavior of if
1 Comment
dpb
on 23 Jan 2023
Edited: dpb
on 23 Jan 2023
ADDENDUM:
You didn't complete the code to be able to draw out the overall structure of the nested IFs, but back to the original Q? re: evaluting first and then executing only the TRUE/FALSE clause based on that initial evaluation...
The outermost IF...ELSE...END expression IS evaluated only initially and only one branch of that clause is executed. The problem is that you nested IF...END structures and those individually obey the same rule; each of them is evaluated when it is reached and only one branch ot that structure will be executed. BUT, those two are completely and utterly independent of each other; the IF() test for the second is totally unaware of the condition of the outer construct; it only "knows about" what it sees at that point; what transpired earlier has no bearing excepting on the values of the variable(s) inside the logical test clause at that instant. Your having changed that value since the prior test now shows up (as it should).
See Also
Categories
Find more on Variables 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!