Error Attempted to access x(2,1); index out of bounds because size(x)=[1,4]

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)

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

i have 30x4 matrix. (30 samples of 4 variables)
due to my regression function, variable 1 has to be multiplied with 0.2075, etc...
i want that in each loop, row with 4 variables should be calculated.
Thanks..

Connectez-vous pour commenter.

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

Thanks for the answer.
The regression function in the code is only my finding by using linear regression.
It becomes fitness function for my GA.
In toolbox help part; ""When the 'Vectorized' option is 'on', fitnessfcn should accept a pop-by-nvars matrix, where pop is the current population size. ""
I have a population of 30 elements and 4 variables.
In my question, only thing i want to do is to take 1st column 1st row to be multiplied by 0.2075, etc... for loop 1. And others are like that.
I think some arrangement can solve this
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];
Thanks for the answer
you re right but when i write as your first equation, it takes complete matrix.
To compensate my equation, i added a comma after for j=1:30
I could not understand your second equation..

Connectez-vous pour commenter.

Question posée :

b
b
le 25 Mai 2012

Community Treasure Hunt

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

Start Hunting!

Translated by