2017-01-14 8 views
0

Kann mir bitte jemand mit meinem Code helfen?Zuordnung des Zelleninhalts zu einem Arrayobjekt ohne Zelle MATLAB

Ich stoße auf diesen Fehler, ich habe sogar einen Schritt-für-Schritt-Ansatz mit MATLAB-Breakpoints versucht, aber immer noch nichts. Jede Hilfe würde sehr geschätzt werden. Dank

Fehler: Zelleninhalt Zuordnung zu einem nicht-Zellen-Array Objekt

Wo: Zeile 52: A = x_train' * x_train + reg_gamma (I) train_l Auge (10,10);

reg_gamma=logspace(-6,3,10); 
rng(50) 

for i = 1:200 

     % Generate random w, x, and noise from standard Gaussian 
     w = randn(10,1); 
     x = randn(600,10); 
     noise = randn(600,1); 

     % Generate the dataset y, using: x'*w+noise 
     y = x*w + noise; 

     % Split data set into a training (100) and a test set (500) 
     x_train=x([1:100],:); 
     x_test=x([101:600],:); 
     y_train=y([1:100],:); 
     y_test=y([101:600],:); 
     train_l = length(y_train); 
     test_l=length(y_test); 

    for j = 1:length(reg_gamma) 

     % 5-fold Cross Validation 
     % From the split use the 100 training for 5-fold CV 
     n = size(x_train,1); 
     k=5; 

     % Split the 100 training into 5 subsets, 4 training and 1 validation 
     % So the training would be 4x20=80 and the validation 20. 
     xvalid{k,1} = []; 
     xtrain{k,1} = []; 
     yvalid{k,1} = []; 
     ytrain{k,1} = []; 

     % Perform the CV 
     chunk = floor(n/k); 

     xvalid{1} = x_train(1:chunk,:); 
     xtrain{1} = x_train(chunk+1:end,:); 
     yvalid{1} = y_train(1:chunk,:); 
     ytrain{1} = y_train(chunk+1:end,:); 

     for f = 2:k 
      xvalid{f} = x_train((f-1)*chunk+1:(f)*chunk,:); 
      xtrain{f} = [x_train(1:(f-1)*chunk,:); x_train(f*chunk+1:end, :)]; 
      yvalid{f} = y_train((f-1)*chunk+1:(f)*chunk,:); 
      ytrain{f} = [y_train(1:(f-1)*chunk,:); y_train(f*chunk+1:end, :)]; 
     end 

     % For every fold calculate the w and the validation score 
     for ff = 1:k 
     A{ff}=xtrain{ff}'*xtrain{ff}+reg_gamma(j)*80*eye(10,10); 
     B{ff}=xtrain{ff}'*ytrain{ff}; 
     w_trainCV{ff}=mldivide(A{ff},B{ff}); 

     sum_validCV{ff}=sum((xvalid{ff}*w_trainCV{ff} - yvalid{ff}).^2); 

     end 

     % Transform the cell arrays to matrix and vectors 
     C = cell2mat(w_trainCV); 

     D = cell2mat(sum_validCV); 
     D = D./20;% 20 is the length of the points for each validation fold 

     % Average w and Validation set for each \gamma 
     w_train(:,j) = mean(C,2); 
     MSE_valid(i,j) = mean(D); 

    end 

    % Check the smallest validation error (M) and find its position (I) 
    [M,I]=min(MSE_valid(1,:)); 

    % Calculate the optimal w (perform RR on gamma with the smallest 
    % validation error) on the new training set (100). 
    % Where (I) the position of the gamma with the smallest validation error. 
    At=x_train'*x_train+reg_gamma(I)*train_l*eye(10,10); 
    Bt=x_train'*y_train; 
    w_train100=mldivide(At,Bt); % The w which we will use on 100 training 

    % Compute the mean squared error on the test set 
    sum_test=sum((x_test*w_train100 - y_test).^2); 
    MSE_test(1,i) = sum_test/test_l; 

end 

UPDATE 1:

es gefunden, das ist, was passiert, wenn Sie zu müde sind, denke ich. Ich benutze A zweimal in meinem Code, also aus einem Zellen-Array mache ich es zu einer Matrix, dann versuche ich als Matrix Matrixinhalte zu speichern.

, dass der Täter war:

% Where (I) the position of the gamma with the smallest validation error. 
    A=x_train'*x_train+reg_gamma(I)*train_l*eye(10,10); 
    B=x_train'*y_train; 
+0

Bitte senden Sie Ihre Antwort als Antwort, und bearbeiten Sie die Frage nicht, es zu schließen. Auf diese Weise werden zukünftige Leser wissen, dass das Problem gelöst ist und nicht versuchen herauszufinden, was Sie fragen. – EBH

Antwort

0

es gefunden, das ist, was passiert, wenn Sie zu müde sind, denke ich. Ich benutze A zweimal in meinem Code, also mache ich aus einem Zellenarray eine Matrix, dann versuche ich als Matrix Matrixinhalte zu speichern.

, dass der Täter war:

% Where (I) the position of the gamma with the smallest validation error. 
    A=x_train'*x_train+reg_gamma(I)*train_l*eye(10,10); 
    B=x_train'*y_train; 
Verwandte Themen