I have a variable in my code called p_fix. It is necessary to save the value of this variable when PE>p. But the way I did it, every time PE>p is respected, the value of this variable is changed, how can I save its value only once when PE>p and never change the value of this variable (p_fix) again?
for n = 1:size(t,1)
if n>=4
X = [Ia(n-1,1) Ia(n-2,1) ; Ia(n-2,1) Ia(n-3,1)];
future = [Ia(n,1) ; Ia(n-1,1)];
C = X\future;
Ia_future(n,1) = C(1,1)*Ia(n,1) C(2,1)*Ia(n-1,1);
PE(n,1)=Ia(n,1) Ia_future(n,1);
p(n,1)=(1 0.2)*max(PE(n-1,1));
if PE(n-1,1)>p(n,1)
p_fix = p(n,1);
end
end
end
CodePudding user response:
You can either:
Before the loop, initialize
p_fix
to an empty array, and only set it if it’s empty:if isempty(p_fix) && PE(n-1,1)>p(n,1) p_fix = p(n,1); end
Or use a second Boolean variable, for example
done
, that keeps track of whether you’ve set your other variable or not. Initialize it tofalse
before the loop, then:if ~done && PE(n-1,1)>p(n,1) p_fix = p(n,1); done = true; end