How do I set counter = to a value when it is a persistent variable so if i reinitialize it each time through a loop it loses it purpose?

조회 수: 2 (최근 30일)
function result = exp_average(a,varargin)
persistent avg counter;
if counter ==0
counter =0;
end
if nargin ==1
in1 = a
no = 1
end
if nargin ==2
in1 = a
in2 = b
no = 2
end
if no ==1 && counter ==0
b = 0.1;
a = in1;
avg = b * a+ (1-b)*avg;
counter = counter +1'
end
if no == 2 && counter ==0
clear exp_average;
a = in1;
b = in2;
avg = b*a+(1-b)*avg;
counter = counter+1;
end
if no ==1 && counter> 0
a = in1;
b = 0.1;
avg = b*a+(1-b)*avg;
counter= counter+1;
end
if no ==2 && counter>0
clear exp_average;
a = in1;
b = in2;
avg = b*a+(1-b)*avg;
counter = counter+1;
end
end
How do I set counter = to a value when it is a persistent variable so if i reinitialize it each time thruugh a loop it loses it purpose? I need to feed "or" and "and" statement logical operators with who numbered values. I'm no so long as counter isn't initialized.

답변 (1개)

Daniel kiracofe
Daniel kiracofe 2016년 11월 28일
I'm not sure if I understand the question. Here is my guess at what you are asking. If this isn't it, please restate the question.
Take a look at
https://www.mathworks.com/help/matlab/ref/persistent.html
The key line is " the persistent variable does not exist the first time you issue the persistent statement, it is initialized to the empty matrix."
So instead of
if counter ==0
counter =0;
end
Try this instead
if (isempty(counter))
counter =0;
end

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by