Loop through a matrix and an equation

조회 수: 2(최근 30일)
Mohamed Asaad
Mohamed Asaad 2022년 9월 26일
댓글: Mohamed Asaad 2022년 10월 2일
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

채택된 답변

William Rose
William Rose 2022년 9월 27일
편집: William Rose 2022년 9월 27일
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))
yhat has size 11 by 1.
Try that. No for loops required.
  댓글 수: 2
Mohamed Asaad
Mohamed Asaad 2022년 10월 2일
@William Rose Thank you very much! It works perfectly.

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

추가 답변(1개)

William Rose
William Rose 2022년 9월 27일
편집: William Rose 2022년 9월 27일
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))
yhat has size 11 by 1.
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
William Rose
William Rose 2022년 9월 27일
@Mohamed Asaad, Oops. I did not read your code carefully enough. Wait a sec.

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

범주

Find more on Particle & Nuclear Physics in Help Center and File Exchange

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by