-1

私は次の 10 倍の実装を持っています。UCI 機械学習によるデータ セット パブリッシュを使用しています。データ セットのリンクは次のとおりです。

Here are my dimensions

x = 

      data: [178x13 double]
    labels: [178x1 double]

これは私が得ているエラーです

Index exceeds matrix dimensions.

Error in GetTenFold (line 33)
    results_cell{i,2} = shuffledMatrix(testRows ,:);

これは私のコードです:

%Function that accept data file as a name and the number of folds
%For the cross fold
function [results_cell] = GetTenFold(dataFile, x)
%loading the data file
dataMatrix = load(dataFile);
%combine the data and labels as one matrix
X = [dataMatrix.data dataMatrix.labels];
%geting the length of the of matrix
dataRowNumber = length(dataMatrix.data);
%shuffle the matrix while keeping rows intact 
shuffledMatrix = X(randperm(size(X,1)),:);

crossValidationFolds = x;
%Assinging number of rows per fold
numberOfRowsPerFold = dataRowNumber / crossValidationFolds;

crossValidationTrainData = [];
crossValidationTestData = [];
%Assigning 10X2 cell to hold each fold as training and test data
results_cell = cell(10,2);
    %starting from the first row and segment it based on folds
    i = 1;
    for startOfRow = 1:numberOfRowsPerFold:dataRowNumber
        testRows = startOfRow:startOfRow+numberOfRowsPerFold-1;
        if (startOfRow == 1)
            trainRows = (max(testRows)+1:dataRowNumber);
        else
            trainRows = [1:startOfRow-1 max(testRows)+1:dataRowNumber];
            i = i + 1;
        end
        %for i=1:10
        results_cell{i,1} = shuffledMatrix(trainRows ,:);
        results_cell{i,2} = shuffledMatrix(testRows ,:); %This is where I am getting my dimension error
        %end
        %crossValidationTrainData = [crossValidationTrainData ; shuffledMatrix(trainRows ,:)];
        %crossValidationTestData = [crossValidationTestData ;shuffledMatrix(testRows ,:)];
    end
end
4

1 に答える 1

6

あなたは毎回ループして1:numberOfRowsPerFold:dataRowNumberいます。これが、でエラーを取得できる方法です。1:x:178iindex out of boundsresults_cell

エラーを取得する別の方法は、範囲testRows外の行を選択することですshuffledMatrix

デバッグを学ぶ

エラーが発生したときにコードを一時停止してデバッグを開始するには、コードをdbstop if error実行する前に実行します。このようにして、エラーが発生するとコンパイラはデバッグ モードになり、問題が発生する直前に変数の状態を調べることができます。

(このデバッグ モードを無効にするには、 を実行しますdbclear if error。)

于 2012-10-04T06:27:26.970 に答える