0

I am working on implementing cross validation at Matlab without using any other functions except for native ones.

I have a matrix like that:

1
2
3
..
N

I have a fold size M

At first iteration I want to take that:

1
2
3
..
N-M

at second iteration:

1
2
3
..
.. //Number o f M elements didn't included here
N-M+1
N-M+2
..
N

iterate until I process

M+1
M+2
..
N

When I don't include any set of elements I want to assign them into another variable or I want to know indexes so I can process them (this one is better for performance)

Further information about cross validation: http://en.wikipedia.org/wiki/Cross-validation_(statistics)

This graphic explains what I want(form Georgia Tech University slides): enter image description here

I am new to matlab, how can I implement it easily?

4

2 回答 2

0

以下代码将分段您的数据,如图所示。

K = 5; %Fold size
N = 25; % Number of data points

data = rand(1,N); % Some fake data

testIdxs = reshape(1:N,K, N/k)'; %now each row has the indices for one test set

% All indices that aren't in the test, should belong to the training set. 
trainIdx = zeros(K, N-K);
for ix = 1:N/k
    temp                = 1:25;
    temp(testIdxs(ix,:)) = [];
    trainIdx(ix,:)      = temp;
end

请记住,此方法仅在 N 是 K 的倍数时才有效。

于 2012-11-19T18:32:10.400 回答
0

一般来说,交叉验证可以通过函数crossvalind来完成。

你可以这样做

Indices = crossvalind('Kfold', matrix , M)
于 2012-11-18T23:03:23.137 回答