a not so complicated task
1 次查看(过去 30 天)
显示 更早的评论
Hey Guys I am trying to solve this out for a coupple of hours but with no succes.
Write a function called exp_average that computes the “exponentially weighted moving average,” or “exponential average” for short, of a sequence of scalars. The input sequence is provided to the function one element at a time and the function returns the current average each time. If we denote the nth element of the input sequence, that is, the function input at the nth invocation, by inn, then the rule for calculating the corresponding average outn that is to be returned by the function is: out1 = in1 outn = b ∙ inn + (1 - b) ∙ outn-1 where b is a coefficient between 0 and 1. You do not need to check b. In plain English, the current average depends on the current input and the previously computed average weighted by b and (1 - b), respectively. Here is how the function is expected to work. When called by two input arguments, the input sequence is reset, the first input argument is considered to be in1 and the second input argument is the value of the coefficient b. When called with a single input argument, it is considered to be int, that is, the current value of the input sequence. In both cases, the output is calculated according to the formula above. If the function is called for the very first time with a single input argument, the value of the coefficient b must default to 0.1. Hint: you should use two persistent variables to store the output and the coefficient b.
This is my code so far and i think it should be working all right:
function [ avr ] = exp_average( i1,i2 )
persistent b;
persistent a;
if nargin>1 && isempty(b)
b=i2; a = i1 ;
elseif nargin<2 && isempty(a) && isempty(b)
b=0.1; a = i1 ;
elseif nargin<2 && ~isempty(a) && ~isempty(b)
a = b*i1+(1-b)*a;
end
avr = a;
end
here are some tables if you find it harder to understand the task, cuz it was hard for me . https://d3c33hcgiwev3.cloudfront.net/imageAssetProxy.v1/e2Nzj3JkEeazqQoyai5dlw_68ec2ac8bf2ad98211a224330e5232a4_matlab_expaverage_flow2.JPG?expiry=1489622400000&hmac=ScMiG_14h58NkR40Naq7uCj9BmTaOUDj0OX2L8yaXAQ
https://d3c33hcgiwev3.cloudfront.net/imageAssetProxy.v1/inPOenJkEeaNlA6zo4Pi2Q_fdc2ed87047057ddc9507f7a71ec8190_matlab_expaverage_flow1.JPG?expiry=1489622400000&hmac=XyMFhJZUHPBY3FYkny_nr-Ug4qgbGYDIGnbg5QJOIvA
1 个评论
Guillaume
2017-3-14
At first glance, your code appears to do what is required so what exactly is your question? Do you not get the correct result (if, so what do you get and what should you get)? Do you get an error (if, so what is the error)?
采纳的回答
更多回答(2 个)
Aniket Tolani
2018-11-4
编辑:Aniket Tolani
2018-11-4
function curr_avg = exp_average(varargin)
if length (varargin)==2 new_avg = varargin{1};
b=varargin{2};
save('avg.mat','new_avg')
save('b.mat','b')
end
if length(varargin)==1 && isempty(ls('avg.mat'))
new_avg=varargin{1};
b=0.1;
save('avg.mat','new_avg')
save('b.mat','b')
curr_avg=new_avg;
return
end
if length(varargin)==1 && ~isempty(ls('avg.mat'))
old_avg=load('avg.mat');
b = load('b.mat');
new_avg = b.b*varargin{1}+(1-b.b)*old_avg.new_avg;
end
curr_avg=new_avg;
end
used this code, but got error message
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!