Creating a loop for genearating matrix

3 views (last 30 days)
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
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
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
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
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?
Divya Krishna Ramakrishna
Divya Krishna Ramakrishna on 22 Dec 2015
this is for 5 values i have written. i need to find for 20 values.

Sign in to comment.

Accepted Answer

Renato Agurto
Renato Agurto on 22 Dec 2015
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)];
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};
Divya Krishna Ramakrishna
Divya Krishna Ramakrishna on 22 Dec 2015
Got it thank you. works perfectly fine :) :)

Sign in to comment.

More Answers (0)


Find more on Special Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!