Creating a loop for genearating matrix

1 次查看(过去 30 天)
Hello All, I am new to matlab and i need to create a loop for my code below. Could yu please help me out in this. The input u and y are 301x1 matrices.
m = 1;
Y1 = y(2:301);
psi1 = [-y(1:300) u(1:300)];
theta1 = inv(psi1'*psi1)*psi1'*Y1
e1 = Y1 - psi1*theta1;
Vmin1 = e1'*e1
m=2;
Y2 = y(3:301);
psi2 = [-y(2:300) -y(1:299) u(2:300) u(1:299)]
theta2 = inv(psi2'*psi2)*psi2'*Y2
e2 = Y2 - psi2*theta2;
Vmin2 = e2'*e2
m=3;
Y3 = [y(4:301)];
psi3 = [-y(3:300) -y(2:299) -y(1:298) u(3:300) u(2:299) u(1:298)];
theta3 = inv(psi3'*psi3)*psi3'*Y3
e3 = Y3 - psi3*theta3;
Vmin3 = e3'*e3
m=4;
Y4 = [y(5:301)];
psi4 = [-y(4:300) -y(3:299) -y(2:298) -y(1:297) u(4:300) u(3:299) u(2:298) u(1:297)];
theta4 = inv(psi4'*psi4)*psi4'*Y4
e4 = Y4 - psi4*theta4;
Vmin4 = e4'*e4
m=5;
Y5 = [y(6:301)];
psi5 = [-y(5:300) -y(4:299) -y(3:298) -y(2:297) -y(1:296) u(5:300) u(4:299) u(3:298) u(2:297) u(1:296)];
theta5 = inv(psi5'*psi5)*psi5'*Y5
e5 = Y5 - psi5*theta5;
Vmin5 = e5'*e5
I need to create a loop for generating psi everytime. Does anyone have any idea about how can we create it for the above code?
  2 个评论
Stephen23
Stephen23 2015-12-22
Why do you need a loop? What is wrong with the code above?
Divya Krishna Ramakrishna
this is for 5 values i have written. i need to find for 20 values.

请先登录,再进行评论。

采纳的回答

Renato Agurto
Renato Agurto 2015-12-22
Hi. I hope this is what you need
N = length(y); %should be 301
for i = 1:5
Y{i} = y(i+1:N);
tmpY = [];
tmpU = [];
for j = 1:i
tmpY = [tmpY -1*y(i+1-j:N-j)];
tmpU = [tmpU u(i+1-j:N-j)];
end
psi{i} = [tmpY tmpU];
theta{i} = inv(psi{i}'*psi{i})*psi{i}'*Y{i};
e{i}= Y{i} - psi{i}*theta{i};
Vmin{i} = e{i}'*e{i};
end
  2 个评论
Divya Krishna Ramakrishna
Thank you. Yes this should work. I will try it and check. Logically its perfect. :) thanks a lot

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Desktop 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by