Difficulty Coming up with a solution, would there be a convenient command for this ??

1 view (last 30 days)
I am looking to implement a for loop but am not quite sure of how to do this given the for loop. I have thought really hard and it may either be due to a lack of skills but am unable to find an answer. Suppose I have a matrix and some predefined variable:
Matrix = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
X = 0.94
What I would like is for matlab to do the following: First construct running mean as follows:
m = zeros(10,1)
for n = 1: m(1,1) = (1-0.94)*(0.94)*(1) = 0.0564
for n = 2: m(2,1) = (1-0.94)*(0.94.^2)*(1) + (1-0.94)*(0.94.^1)(2) = 0.053016
for n = 3: m(3,1) = (1-0.94)*(0.94.^3)*(1) + (1-0.94)*(0.94.^2)(2) + (1-0.94)*(0.94.^1)(3)
for n = 4: m(4,1) = (1-0.94)*(0.94.^4)*(1) + (1-0.94)*(0.94.^3)*(2) + (1-0.94)*(0.94.^2)*(3)+(1-0.94)*(0.94.^1)*(4)
m1 = [0.0564; 0.053016; ... ] %%this is my output matrix
To be precise, I am looking to calculate something similar to the formula where d is equal to X set as 0.94. The calculation above describes the process for calculating the mean but if I get the intuition behind executing it, I can perform it for the variance.
Matrix = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];
X = 0.94;
r = zeros(10);
for t = 1:10
r(t,1) = Matrix(t,1);
for i = 1:10
m(i,1) = (1-X)*(X.^i)*r(t,1);
end
end
This is a code that I came up with but clearly, it is very wrong and I have also tried to index it from t = 10:-1:1, which is also wrong. I have been stuck on this for a very long time and it would be great if someone can help me.
Any help is much appreciated. Thank you

Accepted Answer

Triveni
Triveni on 17 Jan 2016
Edited: Triveni on 17 Jan 2016
I don't know what is your "m1". As i understand. following should be help you
Matrix = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];
X = 0.94;
Matrix1 = [5; 6 ; 7 ; 8 ; 9 ;10 ;0 ;0 ;0 ; 0];
Matrix2 = [4; 6 ; 7; 1; 0 ; 0; 0 ; 0 ; 0 ; 0];
Matrix3 = [1; 2 ; 3; 0; 0 ; 0; 0 ; 0 ; 0 ; 0];
Z = [Matrix Matrix1 Matrix2 Matrix3];
out = zeros(1,1,10);
m1=Matrix1; % I have taken m1=Matrix1
out(:,:,1) = (1-X)*((X.^1)*(Matrix(1,1)-m1(1,1)).^2);
for k=2:10
out(:,:,k) = out((k-1)) + (1-X)*((X.^k+1)*(Matrix(k,1)-m1(k,1)).^2)
end
  3 Comments
Triveni
Triveni on 17 Jan 2016
Edited: Triveni on 17 Jan 2016
for n =1 (1-X)*((X.^1)*(Matrix(1,1)-m1(1,1)).^2).
what did you mean by "m1(1,1)"
Putsandcalls
Putsandcalls on 17 Jan 2016
So it is to take the element from row1,column1 subtracting the calculated matrix from the above procedure.
Iterating the process over, for n=1, it would be:
for n = 1: (1-0.94)*(0.94)*(1) = 0.0564
So for the second step, it would be:
for n =1 (1-X)*((X.^1)*(Matrix(1,1)-m1(1,1)).^2) where m(1,1) = 0.0564.
Sorry for the confusion. I redefined the above comment to make it more clear. Also, the fact that it yields a 3 dimensional matrix is quite problematic. Is there any way to make it so that it is not k-dimensional?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!