필터 지우기
필터 지우기

How to put any variable name automatically?

조회 수: 1 (최근 30일)
Triveni
Triveni 2016년 8월 7일
댓글: Walter Roberson 2016년 8월 8일
In this program,
a = 1 ;
b=2;
c = zeros(20,1);
d= zeros(20,1);
for k = 1:20
c(k) = (b+k)^2 + (a+k)^3;
d(k) = (b+k)^2 - (a+k)^3;
end
[maxobjective, iteration1] = max((c));
but i want to pre define objective function, means
[maxobjective, iteration1] = max((c)); or
[maxobjective, iteration1] = max((d));
put automatically like below.
objfun = c; %or d
a = 1 ;
b=2;
c = zeros(20,1);
d= zeros(20,1);
for k = 1:20
c(k) = (b+k)^2 + (a+k)^3;
d(k) = (b+k)^2 - (a+k)^3;
end
[maxobjective, iteration1] = max((objfun));

채택된 답변

Walter Roberson
Walter Roberson 2016년 8월 7일
Use the same variable for both, but make it a cell array. And make objfun the index of the cell to use.
  댓글 수: 2
Triveni
Triveni 2016년 8월 8일
Ya, done.
objfun = 1; %or 2
a = 1 ;
b=2;
c = zeros(20,1);
d= zeros(20,1);
for k = 1:20
c(k) = (b+k)^2 + (a+k)^3;
d(k) = (b+k)^2 - (a+k)^3;
end
if objfun == 1
[maxobjective, iteration1] = max((c));
end
if objfun == 2
[maxobjective, iteration1] = max((d));
end
Walter Roberson
Walter Roberson 2016년 8월 8일
objfun = 1; %or 2
a = 1 ;
b=2;
c_d{1} = zeros(20,1);
c_d{2} = zeros(20,1);
for k = 1:20
c_d{1}(k) = (b+k)^2 + (a+k)^3;
c_d{2}(k) = (b+k)^2 - (a+k)^3;
end
[maxobjective, iteration1] = max(c_d{objfun});

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by