2014-01-23 3 views
-5

MATLAB에서 GA (유전 알고리즘)와 ANFIS (적응 신경 - 퍼지 추론 시스템)를 비교하는 코드를 발견했습니다. 다음 코드를 실행하면이 오류가 발생합니다.M (1,2)에 액세스하려고 시도했습니다. numel (M) = 1이기 때문에 범위를 벗어난 인덱스

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

저는 처음부터 Matlab의 초보자이므로 문제가있는 곳을 찾을 수 없습니다. 내가 4 라인 5를 주석 때 또한 6과 7 라인에서 오류가 발생

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 

: 다음은 코드입니다. 관심을 가져 주셔서 감사합니다.

+3

오류 메시지는 정확히 어떤 줄이 문제인지 알려줍니다. –

+0

나는 그것을 안다. 그러나 당신의 문제는 정확히 무엇입니까? –

+4

스칼라가 아닌 인수로 342x4 행렬을 전달하려고 시도 했습니까? – Notlikethat

답변

0

numel (M) = 1은 M이 스칼라임을 의미합니다. 따라서 항목 M (1,2)이 없습니다. 따라서 행 4의 존재하지 않는 항목에 액세스하려고 시도하면 오류가 발생합니다.