Effacer les filtres
Effacer les filtres

How to put any variable name automatically?

1 vue (au cours des 30 derniers jours)
Triveni
Triveni le 7 Août 2016
Commenté : Walter Roberson le 8 Août 2016
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));

Réponse acceptée

Walter Roberson
Walter Roberson le 7 Août 2016
Use the same variable for both, but make it a cell array. And make objfun the index of the cell to use.
  2 commentaires
Triveni
Triveni le 8 Août 2016
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 le 8 Août 2016
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});

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by