0
コードを教えてもらえますか?セル以外の配列オブジェクトへのセル内容の割り当てMATLAB
私はこのエラーに遭遇しています。私はさらにMATLABのブレークポイントを使用してステップバイステップのアプローチを試みましたが、まだ何もしていません。どんな助けでも大歓迎です。おかげ
エラー:非セルアレイオブジェクト
にセルの内容の割り当てここで、行52:A = x_train' * x_train + reg_gamma(I)train_l眼(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:
それを見つけ、それはあなたが私が推測あまりにも疲れているとき何が起こっているのです。 私は自分のコードでAを2回使用するので、セル配列からそれを行列に変換し、行列としてセル配列の内容を保存しようとします。犯人だった
:
% 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;
は答えとして、あなたの答えを投稿し、それを含めるように質問を編集しないでください。この方法で将来の読者は問題が解決されたことを知り、あなたが求めていることを見つけようとしません。 – EBH