-5

I found some code in Matlab that is about comparing GA(genetic algorithm) and ANFIS(Adaptive Neuro-Fuzzy Inference System). When I run following code, this error occur :

Attempted to access M(1,2); index out of bounds because numel(M)=1. Error in err (line 4)
m(i)=M(i,2);

Since I'm a beginner in Matlab I can't find where the problem is. Here is the code:

function r=err(M)
for i=1:342;
    s(i)=M(i,1);
    m(i)=M(i,2);
    a(i)=M(i,3);
    b(i)=M(i,4);
end

dat=load('data2.txt','-ascii');
global X Y
X=0; Y=0;
X=dat(:,1);
Y=dat(:,2);


options = gaoptimset(@ga);
opts = gaoptimset(options,'PopulationSize',250);
options.PopInitRange = [0;5];
options.EliteCount = 1;
options.Generations = 250;
options = gaoptimset('PlotFcns', @gaplotbestf);
[param_ga fval] = ga(@temp , 10000 , [],[],[],[],[],[],[],options);

n_data=length(X);

err=0;
for ii=1:n_data
    for jj=1:342
        w(jj)=exp(-1*((X(ii)-param_ga((jj)*m(jj))/(param_ga((jj)*s(jj))))^2));
    end
    sum_w=sum(w);
    ww=w/sum_w;

    y=0;
    for jj=1:342
        y=y+ww(jj)*(param_ga((jj)*b(jj))+param_ga((jj)*a(jj))*X(ii));
    end

    err=err+(y-Y(ii))^2;
end

GA_AVERAGE_ERROR=err



numMFs = 10;
mfType = 'gauss2mf';
epoch_n = 500;
in_fis = genfis1(dat,numMFs,mfType,'linear');
[out_fis,error,stepsize]= anfis(dat,in_fis,epoch_n);

x=min(dat(:,1)):0.1:max(dat(:,1));
y=evalfis(x,out_fis);

yy=evalfis(dat(:,1),out_fis);
e=(yy-dat(:,2)).^2;
E=sum(e)/length(e);


ANFIS_AVERAGE_ERROR=E



if ANFIS_AVERAGE_ERROR>GA_AVERAGE_ERROR
    disp 'ANFIS_AVERAGE_ERROR > GA_AVERAGE_ERROR'
elseif ANFIS_AVERAGE_ERROR<GA_AVERAGE_ERROR
    disp 'ANFIS_AVERAGE_ERROR < GA_AVERAGE_ERROR'
else disp 'ANFIS_AVERAGE_ERROR = GA_AVERAGE_ERROR'
end

n=1:epoch_n;
y=x.^(2.5*sin(1*x))+x.^3-21*x.^2+99*x+21;


figure(1)
scatter(dat(:,1),dat(:,2))
hold on
plot(x,evalfis(x,out_fis),'-r','LineWidth',2);
plot(x,y,'-g','LineWidth',2);
hold off
legend('Training Data','ANFIS Output','Major Function');



disp('                     Result of Program                       ');


disp ('GA_AVERAGE_ERROR      =');disp(GA_AVERAGE_ERROR)
disp ('ANFIS_AVERAGE_ERROR   =');disp(ANFIS_AVERAGE_ERROR)

if ANFIS_AVERAGE_ERROR>=GA_AVERAGE_ERROR
    r=0;
else r=1;
end

if r==1
    disp 'ANFIS METHOD better than GENETIC ALGORITHM METHOD for this case'
else
    disp 'GENETIC ALGORITHM METHOD better than ANFIS METHOD for this case'
end

Also when I commented the 4th line 5th, 6th and 7th line produces errors. Thanks for your attentions, any help appreciated.

IKavanagh
  • 6,089
  • 11
  • 42
  • 47
  • 3
    The error message tells you exactly which line is the problem. – Oliver Charlesworth Jan 23 '14 at 20:35
  • I know that. but what is thee problem exactly? – Mahmood Rasooli Jan 23 '14 at 20:37
  • 4
    Have you tried passing a 342x4 matrix as the argument rather than a scalar? – Notlikethat Jan 23 '14 at 20:39
  • can u tell me how can i do it. i tried err([342,4]) but it gives me another error: Attempted to access M(1,2); index out of bounds because size(M)=[2,1]. – Mahmood Rasooli Jan 23 '14 at 20:48
  • @MahmoodRasuli `[342, 4]` is not a 342-by-4 matrix. It is a 2-element vector. Do you have any matrix data to pass to this function? What exactly are you trying to use this code to do? – gnovice Jan 23 '14 at 20:55
  • sorry about that. I'm not a matlab programmer and my teacher forced me to write code in matlab. I'm good in c# and c++ but he refused to accept the code in those languages. so i must worked it out in couple of days. and this is what happend. – Mahmood Rasooli Jan 23 '14 at 20:56
  • Can someone please tell me how can i define a [342;4] matrix and pass it to this function? – Mahmood Rasooli Jan 23 '14 at 20:59
  • 2
    @MahmoodRasuli You say that this is code you "found" somewhere, and you appear to be trying to apply it to some other problem. Even if you pass it an `M` of the right size, how would you know what values to place in that matrix? And even then, once the code gets to the line where it loads data, that file probably doesn't even exist. This code was written for a very specific purpose, and has no documentation indicating what it requires to work properly. – gnovice Jan 23 '14 at 21:05
  • got it. thanks everyone. i found the data in another file named data.txt and i can load it with load function: load('data2.txt','-ascii'). – Mahmood Rasooli Jan 23 '14 at 21:06

1 Answers1

0

numel(M)=1 means that M is a scalar. Therefore, it does not have an entry M(1,2). Your attempt to access this non-existent entry in line 4 therefore yields an error.

Matt J
  • 1,127
  • 7
  • 15