Used CROSSVALIND to Randomize rows with numerical only but not NAN

1 vue (au cours des 30 derniers jours)
balandong
balandong le 7 Août 2017
Commenté : balandong le 8 Août 2017
Dear all, I have a column double vector which consist 0,1 and NAN. The big idea was to assign the column vector into either one of the group, group 1, group 2,... group 10. The group assignment was realized using CROSSVALIND. However, I only want to assign iff the element is 0 or 1.
For example.
Assume the column double vector
xx = [NaN;NaN;NaN;1;1;;0;0;1;NaN]
Thus, the expected output using CRASSVALIND will be something will
group = [NAN;NAN;NAN;3;4;1;7;4;NAN]
Simply plug in the xx vector as following
Group = crossvalind('Kfold',xx,10);
produce the following error
Error using accumarray
First input SUBS must contain positive integer subscripts.
Thus, the following dirty work is propose
load('xx');
yyy =find (~isnan(xx));
Group = crossvalind('Kfold',yyy ,10);
newGroup =nan (length(xx),1);
for i=1:length(yyy)
newGroup(yyy(i))= Group(i);
end
However, I wanted to know if MATLAB allow better ways to achieve the same goal?
I attached together the MAT file containing the xx vector together with this thread Thanks in advance for the time entertaining this thread.

Réponses (1)

Walter Roberson
Walter Roberson le 7 Août 2017
Not a "better" way, but correcting your code and optimizing slightly:
load('xx');
mask = ~isnan(xx);
Group = crossvalind('Kfold', xx(mask), 10);
newGroup = nan(length(xx),1);
newGroup(mask) = Group;

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by