How to fill array with previous values?

8 views (last 30 days)
George Francis
George Francis on 11 Jun 2018
Answered: George Francis on 11 Jun 2018
Hello guys I have data which contains 2 columns and 600 rows. It basically contains inputs u and outputs y. And my question is how can I fill array 3x1 as follows:
z=[-u(n)-2*u(n-1)-u(n-2);y(n-2)-y(n-1);y(n)-y(n-1)];
I know that I have to make loop but my loop which is below isn't correct and I don't know how to fix it. So can you please help me?
N=length(y);
for n=1:N
for i=1:2
W(i) = y(n-i); %output
end
for i=1:2
V(i) = u(n-i+1); %input
end
z = [V';W'];
end

Answers (2)

KSSV
KSSV on 11 Jun 2018
A = rand(600,2) ; % some random data
u = A(:,1) ; y = A(:,2) ;
n = 1:600 ;
z = zeros(600,3) ;
for n = 3:600
z(n,:) = [-u(n)-2*u(n-1)-u(n-2) y(n-2)-y(n-1) y(n)-y(n-1)];
end
z(1:2,:) = [] ;
Note, the above can be achieved without loop also.
  2 Comments
George Francis
George Francis on 11 Jun 2018
the problem is z has to be array 3x1 when I tried your version z is 598x3.
KSSV
KSSV on 11 Jun 2018
As z is inside loop, every loop value has been saved..

Sign in to comment.


George Francis
George Francis on 11 Jun 2018
I'm making some other calculations from z so z has to be 3x1 in that loop. I actually tried something see code below. But I don't know if it's working correctly. I mean it works but I don't know if that is solution which I was looking for.
delta = 0.002;
sigma=1;
lamda=1;
C = sigma*eye(3);
P = [1;1;1];
N=length(y);
for n = 4:N
for i=1:3
W(i) = y(n-i);
end
y_n=W(1,1);
yn_1=W(1,2);
yn_2=W(1,3);
for i=1:3
D(i) = u(n-i);
end
u_n=D(1,1);
un_1=D(1,2);
un_2=D(1,3);
z = [-u_n-2*un_1-un_2;yn_2-yn_1;y_n-yn_1];
rk=-4*delta^2*yn_1;
C = (C - ( (C*z*z'*C)/( lamda+(z'*C*z) ) ))/lamda;
P = P + C*z*(rk - (z'*P))/(lamda+(z'*C*z));
end

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!