Search code examples
arraysmatlabcross-validationcell-array

MATLAB crossvalind function throws error on cell array


I'm trying to build HMM model baesed on discrete sequences. meaning I have cell array with each cell containing a sequence of different length.

For example:

seqs{1} = 2     3     6     4     7     1     5    13    14    15    16     0     0     0     0    12    
seqs{2} = 2     3     6     4     7     1     5    19    10    11    13  
seqs{3} = 2     3     6     4     7     1     5    19    10  
seqs{4} = 2     3     6     4     7     1     5    19    10    12  

Now, I want to use matlab crossvalind function for K-fold cross validation.

when I'm trying to run indices = crossvalind('Kfold',seqs,2); I get the following error:

Error using grp2idx (line 106) A grouping variable must be a categorical, numeric, logical, datetime or >duration vector, a cell vector of strings, or a 2D character array.

I need each cell (cell contains sequence) to belong to a group from the k groups (of the k-folds).

For example (for k=2 folds):

seqs{1}  - Group 1  
seqs{2}  - Group 2  
seqs{3}  - Group 2  
seqs{4}  - Group 1   

This error does not appear when I'm using a matrix, but since I must use a cell array, matrix is not an option.

How can I do cross validation on cell array?


Solution

  • Why don't you just use the size of seqs to generate the indices

    indices = crossvalind('Kfold',numel(seqs),2);