필터 지우기
필터 지우기

How to fill array with previous values?

조회 수: 4 (최근 30일)
George Francis
George Francis 2018년 6월 11일
답변: George Francis 2018년 6월 11일
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

답변 (2개)

KSSV
KSSV 2018년 6월 11일
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
George Francis
George Francis 2018년 6월 11일
the problem is z has to be array 3x1 when I tried your version z is 598x3.
KSSV
KSSV 2018년 6월 11일
As z is inside loop, every loop value has been saved..

댓글을 달려면 로그인하십시오.


George Francis
George Francis 2018년 6월 11일
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

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by