Loop through a matrix and an equation
3 views (last 30 days)
Show older comments
Mohamed Asaad
on 26 Sep 2022
Commented: Mohamed Asaad
on 2 Oct 2022
Hello,
I need to loop through the matrix X and then convert the (-1 0 1) to actual value for three different variables (P, T, q). The first column in the matrix belongs to P, the second belongs to P and the third to q. These values will be then put in the equation in the last row to get the result of y_hatt in an array. Could anybody help me with that?
I know that the way i am sharing to solve the problem is totally wrong but I am just trying to show how I am thinking to solve it.
clc, clear
y_i = [72.1 165.14 32.56 135.84 77.7 208.11 33.57 174.57 147.19 147.44 147.05 208.54 -117.16 80.76 128.61 169.67 113.13 ];
beta = [148.54 73.564 14.534 -16.385 -11.56 -31.214 10.808];
X = [-1 -1 -1; 1 -1 -1; -1 1 -1; 1 1 -1; -1 -1 1; 1 -1 1; -1 1 1; 1 1 1; 0 0 0; 0 0 0; 0 0 0];
for i = 1:size(X,1)
if(P(i,1) == -1)
P = 0.1;
elseif (P(i,1) == 0)
P = 1;
else
P = 2;
end
if(T(i,2) == -1)
T = 298;
elseif (T(i,2) == 0)
T= 400;
else
T = 420;
end
if(q(i,3) == -1)
q = 0.0001;
elseif (q(i,3) == 0)
q = 0.00001;
else
q = 0.0005 ;
end
y_hatt = beta(1) + beta(2)*P(i) + beta(3)*q(i) + beta(4)*T(i) + beta(5)*T(i)^2 + beta(6)*P(i)^2 + beta(7)*(P(i)*q(i)) ;
% X_final = new value matrix
end
0 Comments
Accepted Answer
William Rose
on 27 Sep 2022
Edited: William Rose
on 27 Sep 2022
clear
y_i = [72.1 165.14 32.56 135.84 77.7 208.11 33.57 174.57 147.19...
147.44 147.05 208.54 -117.16 80.76 128.61 169.67 113.13 ];
beta = [148.54 73.564 14.534 -16.385 -11.56 -31.214 10.808];
X = [-1 -1 -1; 1 -1 -1; -1 1 -1; 1 1 -1; -1 -1 1; 1 -1 1;...
-1 1 1; 1 1 1; 0 0 0; 0 0 0; 0 0 0];
p=X(:,1);
P=0.1*(p==-1)+1*(p==0)+2*(p==1);
t=X(:,2);
T=298*(t==-1)+400*(t==0)+420*(t==1);
qq=X(:,3);
q=1e-4*(qq==-1)+1e-5*(t==0)+5e-4*(t==1);
Xaug=[ones(size(P)),P,q,T,T.^2, P.^2, P.*q];
yhat=Xaug*beta';
fprintf('yhat has size %d by %d.\n',size(yhat))
Try that. No for loops required.
2 Comments
William Rose
on 27 Sep 2022
In case the code above is not clear, let's examine some of the intermediate values.
X = [-1 -1 -1; 1 -1 -1; -1 1 -1; 1 1 -1; -1 -1 1; 1 -1 1;...
-1 1 1; 1 1 1; 0 0 0; 0 0 0; 0 0 0];
p=X(:,1);
disp(p')
P=0.1*(p==-1)+1*(p==0)+2*(p==1);
disp(P');
You can see that p is the vector of -1, 0, and +1 values.
P is p converted to values +0.1, +1.0, and +2.0.
t and T, and qq and q, work the same way.
More Answers (1)
William Rose
on 27 Sep 2022
Edited: William Rose
on 27 Sep 2022
I assume that when you wrote "the second belongs to P" you meant to write "the second belongs to T".
Your equation is
yhat = beta1 + beta2*P + beta3*q + beta4*T + beta5*T^2 + beta6*P^2 + beta7*P*q
where beta is a 1x7 (row) vector; P, T, q are 11x1 vectors; and X=[P,T,q] is an 11x3 array.
Let us define a new matrix Xaug, size 11x7, where
Xaug=[ones, P, q, T, T.^2, P.^2, P.*q], where ones, P, T, q, T.^2, P.^2, and P.*q are column vectors.
Then yhat=Xaug*beta', where yhat is a 11x1 (column) vector and beta'=transpose of beta.
clear
y_i = [72.1 165.14 32.56 135.84 77.7 208.11 33.57 174.57 147.19...
147.44 147.05 208.54 -117.16 80.76 128.61 169.67 113.13 ];
beta = [148.54 73.564 14.534 -16.385 -11.56 -31.214 10.808];
X = [-1 -1 -1; 1 -1 -1; -1 1 -1; 1 1 -1; -1 -1 1; 1 -1 1;...
-1 1 1; 1 1 1; 0 0 0; 0 0 0; 0 0 0];
P=X(:,1); T=X(:,2); q=X(:,3);
Xaug=[ones(size(P)),P,q,T,T.^2, P.^2, P.*q];
yhat=Xaug*beta';
fprintf('yhat has size %d by %d.\n',size(yhat))
You have only 11 rows in X, therefore yhat has 11 rows. But you have 17 values for y_i. This does not seem sensible. Maybe there are more rows in X and you just didn;t want to write them all yet.
Try it. Good luck.
1 Comment
William Rose
on 27 Sep 2022
@Mohamed Asaad, Oops. I did not read your code carefully enough. Wait a sec.
See Also
Categories
Find more on Loops and Conditional Statements 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!