# Loop through a matrix and an equation

4 ビュー (過去 30 日間)
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 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 件のコメント表示非表示 1 件の古いコメント
Mohamed Asaad 2022 年 10 月 2 日
@William Rose Thank you very much! It works perfectly.

サインインしてコメントする。

### その他の回答 (1 件)

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".
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 2022 年 9 月 27 日

サインインしてコメントする。

### カテゴリ

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