0

I'd like to write the Matlab code for the following equation:

z(k)=lamda*x(k)+(1-lamda)*z(k-1)

lamda can be of any value. x is a 1000x22 matrix. Z(0)=0.

Can anyone help me please?

m7913d
  • 10,244
  • 7
  • 28
  • 56
  • Possible duplicate of [Recursive Anonymous Function Matlab](https://stackoverflow.com/questions/32237198/recursive-anonymous-function-matlab) – m7913d Jul 08 '17 at 10:35
  • The most important reason why your code doesn't work is because you do not specify a stop condition. See the duplicate for how this can be done. – m7913d Jul 08 '17 at 10:37
  • The close vote as "too broad" is not justified. it's a low quality question maybe. But if it would be a little more specific, it could be clearly answered. – Robert Seifert Jul 08 '17 at 20:44

2 Answers2

2

You can use iteration function.same thing like this

function z = itrationFunctio(k,x,lambda)
if(k == 0)
    z = 0;
else
    z = lambda*x+(1-lambda)*itrationFunctio((k-1),x,lambda);
end

and in your code just call itrationFunctio(k,x,lambda).

Saman
  • 2,506
  • 4
  • 22
  • 41
0

Does that vectorized solution work for you?

% parameters
x = rand(10,1);
lambda = 2;

% init z, normalize with (1-lambda)
z(2:numel(x)) = lambda/(1-lambda)*x(2:end);

% cumsum, denormalize with (1-lambda)
z = cumsum(z)*(1-lambda)

However I don't get why your x is a matrix and not a vector. What is z supposed to be, in what dimension works k? So in case xrepresents a couple of vectors you want to calculate in parallel, that could work:

% parameters
x = rand(1000,22);
lambda = 2;

% init z, normalize with (1-lambda)
z(2:size(x,1),:) = lambda/(1-lambda)*x(2:end,:);

% cumsum, denormalize with (1-lambda)
z = cumsum(z,1)*(1-lambda)
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113