# Matlab feature selection

I am trying to learn relevant features in a 300*299 training matrix by taking a random row from it as my test data and applying sequentialfs on it. I have used the following code:

>> Md1=fitcdiscr(xtrain,ytrain);
>> func = @(xtrain, ytrain, xtest, ytest) sum(ytest ~= predict(Md1,xtest));
>> learnt = sequentialfs(func,xtrain,ytrain)


xtrain and ytrain are 299*299 and 299*1 respectively. Predict will give me the predicted label for xtest(which is some random row from original xtrain).

However, when I run my code, I get the following error:

Error using crossval>evalFun (line 480)
The function '@(xtrain,ytrain,xtest,ytest)sum(ytest~=predict(Md1,xtest))' generated the following error:
X must have 299 columns.

Error in crossval>getFuncVal (line 497)
funResult = evalFun(funorStr,arg(:));

Error in crossval (line 343)
funResult = getFuncVal(1, nData, cvp, data, funorStr, []);

Error in sequentialfs>callfun (line 485)
funResult = crossval(fun,x,other_data{:},...

Error in sequentialfs (line 353)
crit(k) = callfun(fun,x,other_data,cv,mcreps,ParOptions);

Error in new (line 13)
learnt = sequentialfs(func,xtrain,ytrain)


Where did I go wrong?

• Call Md1=fitcdiscr(xtrain,ytrain); inside your "func" function. Save "func" in a seperate file if necessary. Remember, "sequentialfs" will pass 4 arguments (XTRAIN,ytrain,XTEST,ytest) to func. MATLAB is doing a 10-fold CV if you haven't disabled it already. Why would you need that single observation as a test set? – Sal Nov 15 '16 at 4:28
• MATLAB is doing that cross-validation already. You don't need to do it yourself. Just ,make sure you did NOT set 'cv' = 'none' somewhere in your code. – Sal Nov 15 '16 at 4:45
• ok, got it. So my function handle shuld look like this, right: f = @(xtrain, ytrain, xtest, ytest) sum(ytest ~= predict(fitdiscr(xtrain,ytain),xtest))); ? – Apurv Nov 15 '16 at 4:53