Error Attempted to access x(2,1); index out of bounds because size(x)=[1,4]
Afficher commentaires plus anciens
I am using Genetic Algorithm Toolbox(GUI). My fitness function is;
function y=regressionfcn(x)
for j=1:30
y= -0.0249 - 0.2075* x(j,1) - 0.3313* x(j,2) - 0.0731* x(j,3) - 0.0738* x(j,4);
end
end
But there occurs error as
Error Attempted to access x(2,1); index out of bounds because size(x)=[1,4]
Réponses (2)
Sean de Wolski
le 25 Mai 2012
0 votes
Apparently the regression function is fed a row vector rather than a column vector. To fix this, use:
J(1,2) or to be even safer just use j(2).
1 commentaire
b
le 25 Mai 2012
Walter Roberson
le 25 Mai 2012
0 votes
The fitness function for ga is expected to take a row vector as input.
Are you really trying to fit 120 parameters?? Is there any reason you are not using a simple linear regression instead of ga() ?
4 commentaires
b
le 25 Mai 2012
b
le 25 Mai 2012
Walter Roberson
le 25 Mai 2012
function y=regressionfcn(x)
y= -0.0249 - 0.2075 * x(:,1) - 0.3313 * x(:,2) - 0.0731 * x(:,3) - 0.0738 * x(:,4);
end
This should be more efficient. Also note that your previous code overwrote "y" in each loop iteration.
The body could, I think, be made even more efficient as just
y = [ones(size(x,1),1) x] * [-0.0249 -0.0275 -0.3313 -0.0731 -0.0738];
b
le 25 Mai 2012
Catégories
En savoir plus sur Genetic Algorithm dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!