Unable to store the persistent variable

2 ビュー (過去 30 日間)
Astrik
Astrik 2016 年 8 月 31 日
編集済み: Azzi Abdelmalek 2016 年 9 月 1 日
I am trying to store two persistent variables in order to use them in further iterations. The problem is with the second variable 'weight'. It has a default value 0.1. Once I give a value to my function's 2nd argument, it should take this value.
The code is
function out =exp_average(in,b)
persistent average weight
if isempty(weight) %It does not already exist
weight = 0.1;
end
if isempty(average)
average=in;
elseif nargin<2
average=weight*in+(1-weight)*average;
elseif nargin==2
clear exp_average
average=in;
average=b*in+(1-b)*average;
weight=b;
else
error('WTF')
end
out=average;
end
When I give two arguments to my function, it want my persistent variable 'weight' take the value of the input argument 'b'. With this code it does not keep its value. Any help will be appreciated.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 9 月 1 日
編集済み: Azzi Abdelmalek 2016 年 9 月 1 日
function out =exp_average(in,b)
persistent average weight
if isempty(weight) %It does not already exist
weight = 0.1;
average=in;
end
if nargin==1
average=weight*in+(1-weight)*average;
elseif nargin==2
average=b*in+(1-b)*in;
weight=b;
else
error('WTF')
end
out=average;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by