How to write a For function using values from a table ?

조회 수: 8 (최근 30일)
Rami Altam
Rami Altam 2018년 3월 28일
댓글: Rami Altam 2018년 3월 28일
My equation is:
y(1) = FOPT -(55930.4 - 330502.*x(1) - 3.18673E6.*x(2) + 344393.*x(3) + 471232.*x(4) + 1.58821E6.*x(5) - 156.186.*x(6));
I want to create a For function and substitute FOPT of a value from a table in the Matlab named FOPT.
Thank you.

답변 (1개)

Walter Roberson
Walter Roberson 2018년 3월 28일
y = NameOfTable.FOPT - (55930.4 - 330502.*x(1) - 3.18673E6.*x(2) + 344393.*x(3) + 471232.*x(4) + 1.58821E6.*x(5) - 156.186.*x(6));
All of the x references are scalars, so the expression in () is a scalar, and a scalar can be subtracted from a matrix without difficulty.
Note: if you are trying to fit parameters against a model, then unless you have strong constraints on your x values, there is not going to be a unique answer, since a change in any one of the x() values can be exactly compensated by a change in another value. For example an increase of 1 in x(1) can be exactly compensated by a decrease of 330502 / 156.186 = 2116.07954618212 in x(6) or an increase of 344393/330502 = 1.04203000284416 in x(3)
  댓글 수: 3
Walter Roberson
Walter Roberson 2018년 3월 28일
편집: Walter Roberson 2018년 3월 28일
FOPT_values = NameOfTable.FOPT;
num_FOPT = length(FOPT_values);
y = zeros(num_FOPT, 1);
for FOPT_idx = 1 : num_FOPT
FOPT = FOPT_values(FOPT_idx);
y(FOPT_idx) = FOPT -(55930.4 - 330502.*x(1) - 3.18673E6.*x(2) + 344393.*x(3) + 471232.*x(4) + 1.58821E6.*x(5) - 156.186.*x(6));
end
... But this is a waste of time, since you can use the simple code
y = NameOfTable.FOPT - (55930.4 - 330502.*x(1) - 3.18673E6.*x(2) + 344393.*x(3) + 471232.*x(4) + 1.58821E6.*x(5) - 156.186.*x(6));
This does substitute FOPT from the table named NameOfTable
Rami Altam
Rami Altam 2018년 3월 28일
Thank you

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

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by