单元格内容分配给非单元格数组对象MATLAB

时间:2017-01-14 20:16:02

标签: arrays matlab cross-validation

有人可以帮我解决我的代码吗?

我遇到了这个错误,我甚至尝试了使用MATLAB断点的逐步方法,但仍然没有。任何帮助将非常感激。感谢

错误:将单元格内容分配给非单元格数组对象

其中:第52行:A = x_train'* x_train + reg_gamma(I) train_l eye(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

更新1:

发现它,这就是当你太累了我发生的事情。 我在代码中使用A两次,因此从单元格数组中我将其转换为矩阵,然后作为矩阵我尝试保存单元格数组内容。

那是罪魁祸首:

% 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;

1 个答案:

答案 0 :(得分:0)

发现它,这就是当你太累了我发生的事情。我在代码中使用A两次,因此从单元格数组中我将其转换为矩阵,然后作为矩阵我尝试保存单元格数组内容。

那是罪魁祸首:

% 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;