MATLAB: A not so complicated task

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

Best Answer

One hour later i did it .
function [ avr ] = exp_average( i1,i2 )
persistent b;
persistent a;
if nargin>1 && isempty(b)
b=i2; a = i1 ;
elseif 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