0
votes

Can someone please help me with my code?

I am encountering that error, I even tried a step-by-step approach using MATLAB's breakpoints, but still nothing. Any help would be much appreciated. Thanks

Error: Cell contents assignment to a non-cell array object

Where: Line 52: A=x_train'*x_train+reg_gamma(I)train_leye(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:

Found it, that's what's happening when you are too tired I guess. I use A twice in my code, thus from a cell array I turn it into a matrix, then in as a matrix I try to save cell-array contents.

That was the culprit:

% 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
Please post your answer as an answer, and don't edit the question to include it. This way future readers will know that the problem is solved, and won't try to find out what it is that you'r asking. - EBH

1 Answers

0
votes

Found it, that's what's happening when you are too tired I guess. I use A twice in my code, thus from a cell array I turn it into a matrix, then in as a matrix I try to save cell-array contents.

That was the culprit:

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