View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Multi Class Support Vector Machine

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

3.8 | 17 ratings Rate this file 109 Downloads (last 30 days) File Size: 2.17 KB File ID: #33170 Version: 1.4
image thumbnail

Multi Class Support Vector Machine


Anand Mishra (view profile)


07 Oct 2011 (Updated )

This function removes out the limitation of MATLAB SVM function of two class and uses more classes.

| Watch this File

File Information

This function can classify more than two classes which is limited in MATLAB SVM. This is primary work and does not include plotting function for SVM.
This is version 3.0 of original function which removes some limitations of first & Second one.


This file inspired Multi Class Svm.

Required Products Bioinformatics Toolbox
Statistics and Machine Learning Toolbox
MATLAB release MATLAB 7.12 (R2011a)
MATLAB Search Path
Other requirements Bioinformatics Toolbox only.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (53)
25 Aug 2016 Ayush Pandey  
19 Aug 2016 Abdul Latif

i have problem with my code. I want to make three class or groups in svm, but this code don't work.
Can anybody help me please?
Thank you very much.

Code :
GLCM2 = graycomatrix(I3,'Offset',[4 4]);
stats = graycoprops(GLCM2,{'contrast','homogeneity','energy','Correlation'})

a = stats.Contrast;
b = stats.Correlation;
c = stats.Energy;
d = stats.Homogeneity;

set(handles.edit1, 'String',a);
set(handles.edit2, 'String',b);
set(handles.edit3, 'String',c);
set(handles.edit4, 'String',d);

load class_uji2.txt;// document text class or group with three class {0,1,2}
load data_uji2.txt;// document training data
massa = svmtrain(uji1,class_uji2)

uji = svmclassify(massa, [a b c d]) // [a b c d] test data with glcm
if uji == 0
hasil = 'Normal'
else if uji == 1
hasil = 'Benign Cancer'
hasil = 'Malignant Cancer'

set(handles.edit5, 'String',hasil);

Comment only
27 Jun 2016 hello_world

What is the size of input variable 'C' in your code? I am not sure if it is equal to the size of training set, or to the size of raw data set?

How to perform 10 fold cross-validation?

How to use custom kernel?

Comment only
03 May 2016 Sneha Jain

hii...i am doing retrieval of images..i have classified my target image using code of Cody which is a modified version of your code...there are three classes namely, 1,2 and 3..the class of my target image is displayed as 1. now i want to retrieve all images of class 1 and display the images. how to do that??

Comment only
13 Apr 2016 Anand Mishra

Anand Mishra (view profile)

@can liu
The other submission of this file by user Cody shows the result as
I don't know whether his code is wrong or whether it is systems error.

Comment only
13 Apr 2016 Anand Mishra

Anand Mishra (view profile)

No idea about plotting multisvm now, will work for plotting in free times.
Read Comments inside file for train and other informations.
@saadoon al-sumaidaee
Thanks to Manu BN to resolve your query.
@mesut maleki
Check for Previous Comments you'll got answer.
@aakaash vaidyanathan
It is working in present version also..if you are unable to use it then you can download its toolbox available with latest Matlab version or download and update from the link above in page.
@can liu
Sorry Can Liu, I have run your code and got correct result. Don't know why your system gave incorrect answer. If here will get same incorrect result then share here. I will look further for problems.

Comment only
07 Apr 2016 can liu

I think there is an error in your code
R=[1 2 3 4;1 2 3 4;3 4 5 6;7 8 9 10;7 8 9 10]
C=[2 2 1 3 3]'
T=[1 2 3 4;7 8 9 10;3 4 5 6]

result:IT1 =

the right answer should be
I think the error found in your code

%classify test cases
for j=1:size(TestSet,1)
for t=1:numClasses
result(j) =t;

when t=1,will happen error .
i am looking forward to your relay

Comment only
06 Apr 2016 aakaash vaidyanathan

in present matlab mutisvm function not working, then how to train and use the svm??
its coming Undefined function or variable 'multisvm'. please help

Comment only
10 Oct 2015 mesut maleki

i have this error. when features are 480*25. when i have 480*20 it is correctly work!!!
please help me
Error using seqminoptIseqminoptImpl (line 198)
No convergence achieved within maximum number of iterations.

Error in seqminopt (line 81)
[alphas offset] = seqminoptImpl(data, targetLabels, ...

Error in svmtrain (line 505)
[alpha, bias] = seqminopt(training, groupIndex, ...

Error in multisvm (line 20)
models(k) = svmtrain(TrainingSet,G1vAll);

Error in SVM_4tayy (line 233)

24 Aug 2015 Manu BN

Manu BN (view profile)

Plz note that the group must strictly be a row vector. For e.g to classify into three classes: (where each class is of 10*13)

T = [Class1;Class2;Class3];
C = [ zeros(1,10) ones(1,10) 2*ones(1,10) ];

The dimensions of T is 30*13
The dimensions of C is 1*30

Guess u got the point !!!

15 Jun 2015 saadoon al-sumaidaee

Hi all,
Just I need to know is the Train set dimension is considered or no?
i.e. I have a vector of features, each vector length has equal to 32256, and I have a six class. Can I use multi class in the classifying the facial expressions?

Comment only
30 May 2015 Tung Le  
30 Mar 2015 KALYAN KUMAR

what exactly group train is??
can i know train data and test data matrix sizes for example?

Comment only
20 Mar 2015 aarini

aarini (view profile)

How to plot the classified result? Kindly help.

Comment only
04 Mar 2015 Anand Mishra

Anand Mishra (view profile)

@kamil kamil, @ LEON, @chaou, @ Anirban, @ M@lik Ali, @ yavuzca23, @ Niyalatul, @ Mehrdad Azh, @ Subha, @ jamila, @ Aya, @ billel madridi, @ Weizhi Li, @ raha, @ mustafa alnasser, @ jie, @ shila, @ sunil ds, @ Roshni rahul
Sorry Everyone again, for late reply because of very busy schedule I am this much late to reply.
Not able to answer every query, I would suggest everyone to read answers given by many other members in Commenets to solve many your problems before asking.
I think many of you are not finding example of file in Preview Image above. For you I am giving 1 example given by user Cody.I appreciate Cody work.

%% SVM Multiclass Example
% SVM is inherently one vs one classification.
% This is an example of how to implement multiclassification using the
% one vs all approach.
T=[ 1 10;2 20;3 30;4 40;5 50;6 66;3 30;4.1 42];
test=[3 34; 1 14; 2.2 25; 6.2 63];
results = multisvm(T, C, test);
disp('multi class problem');

Thank You for taking help.

Comment only
05 Feb 2015 Roshni rahul

sir,im doing my thesis on facial expression recognition.i am using CK+ dataset.can use please tell me how to create training set,test set and group set as i want to classify expression into 6 classes ie anger,surprise,sad,disgust,happiness and Fear.

19 Jan 2015 sunil

sunil (view profile)

sir, pl inform me how to find input values to the training set, test test and group test?

Comment only
15 Jan 2015 sunil

sunil (view profile)

sir, i'm new to matlab. how to find values to give test set and training set.

Comment only
04 Nov 2014 shila

shila (view profile)

Hello sir. i have used this function. i have to classify 3 classes. but its not getting expected result.. its shows only one class its unable to group rest two classes.. whts the problme?

Comment only
27 Oct 2014 jie

jie (view profile)

Could you please tell me the reference of your aglorithm? Thanks a lot.

24 Sep 2014 mustafa alnasser

I am using neural network for classification of four classes and I am trying to use SVM to do the same analysis but I do not how to reflect 4 input vectors on the three sets (group, test.)

Comment only
28 May 2014 Ansuman Mahapatra  
16 Jan 2014 raha

raha (view profile)

i am new in svm
i wonder how calculate accuracy in this algorithm
plz help me

Comment only
01 Oct 2013 Weizhi Li

Error using seqminopt>seqminoptImpl (line 198)
No convergence achieved within maximum number of iterations.

Error in seqminopt (line 81)
[alphas offset] = seqminoptImpl(data, targetLabels, ...

Error in svmtrain (line 499)
[alpha, bias] = seqminopt(training, groupIndex, ...

Error in multisvm (line 20)
models(k) = svmtrain(TrainingSet,G1vAll);

I think it does not work

12 May 2013 billel madridi

hey ; why i can't see the file could you send it to me to( )
please i really need it it's emergency

24 Apr 2013 Aya

Aya (view profile)

please i need to know it's one vs all or one vs one relation??

Comment only
13 Apr 2013 jamila

jamila (view profile)

thank you for this code, it great ; but i have an error when i try to change the kernel function. The error is " Error using ==> seqminopt>seqminoptImpl at 203
No convergence achieved within maximum number of iterations.

Error in ==> seqminopt at 84
[alphas offset] = seqminoptImpl(data, targetLabels, ...

Error in ==> svmtrain at 541
[alpha bias] = seqminopt(training, groupIndex, ...

Error in ==> multisvm at 49
svmStruct = svmtrain(T,newClass,'kernel_function','linear' ); %
I am using rbf kernel function, you must change it also

Error in ==> teststa at 61
[itrfin] = multisvm( data,classa,test )"

can you help me please?

Comment only
06 Feb 2013 Subha

Subha (view profile)

is it possible to plot confusion matrix for this program... if so how to do tat.. we have tried many combinations but does not working...
else any method to calculate the performance of the overall system from test data's..

Comment only
11 Jan 2013 Mehrdad Azh


Could you please upload a test case for us to see how to use it?


11 Jan 2013 Mehrdad Azh


Could you please upload a test case for us to see how to use it?


Comment only
21 Dec 2012 Niyalatul

I'm still new to learn this topic.
How can I learn multiclass svm more specific (literature suggestions that I can use, like the books and other)?


Comment only
08 Dec 2012 Cody

Cody (view profile)

  • 1 file
  • 3.66667

Please see my revision of this code here:

Comment only
07 Dec 2012 Cody

Cody (view profile)

  • 1 file
  • 3.66667

Hello, thanks for your contribution. It helped me out, but it was confusing and I have re-written and uploaded a clearer function that does the same thing. It is only about 15 lines of code. I'll provide a link when it is approved.

07 Dec 2012 Cody

Cody (view profile)

  • 1 file
  • 3.66667
08 Nov 2012 yavuzca23

does this code work with 6 inputs which include 3200 samples , and 30 classes(outputs)?

Comment only
14 Aug 2012 M@lik Ali

Can any one tel me how i can run this program, please i am new in mat-lab.
what and how to give input to the function,
i have a feature vector of the images how i can use it.

Comment only
13 Jul 2012 Anirban

Hey Anand

Thank for sharing 'multisvm' code, its really useful! But can you tell is there any way to get the learned models so that while testing for next time I dot need to train again. Because training in every test step is pretty time consuming.

Thanks in advance for your suggestion

Comment only
26 Jun 2012 MML

MML (view profile)

thanks a lot Anand for sharing your code.
I found it very useful.

@Chaou and @Yap
The class-vector (C) should be a row vector. I had the same problem when I tried to use a column vector for C. I hope this resolves the problem.

29 May 2012 Yap

Yap (view profile)

me too, my result output all 1 1 1 1.....I hope some1 could help!

27 May 2012 Chaou

Chaou (view profile)

Hello, I'd like first to thank you for this program. But unfortunately, it doesn't work in my case.
I'm obtaining a itrfin=1 in all cases. I'm using a test vector of 30 elements, and I obtain a itrfin vector = 1 1 1 1.... 1.

Why ? I really appreciate your help. Thanks !

20 Apr 2012 Shikha

Shikha (view profile)

12 Apr 2012 LEONG

LEONG (view profile)

I am searching a way of plotting the multi-svm.

Description about my project:
My application is actually is to classify 4 four classes of different input signals. i have three trial for each subject which each of them need to four different of movement..

Here is my some questions:

1. Does multi-svm that Anand provided able to plot the graph?
2. If yes, how to plot it ..i mean the algorithm..
3. beside, i want to know whether the multi-svm support for classperf ? because i test it before.. it come out with error as:

??? Error using ==> classperf at
The classifier output CLASSOUT
does not have the same size
as the ground truth and there was
not any TESTIDX provided.

Error in ==>
multisvm_classification2 at 63

4. What does that mean..i got find the error at internet but the the problem still did'nt solve..
any solution for it ?

5. Can i use more than two training data but with one test in multi-svm version 2.0 or 3.0 ?can i use it to obtain classperformance of multi-training and test for many data?

Your suggestion will be appreciated..
thank you

Comment only
05 Apr 2012 kamil kamil

Thank you for this program.
How we can determine the best (Sigma, C) with this method ?

Comment only
04 Apr 2012 Anand Mishra

Anand Mishra (view profile)

Download Version 3.0 of this function and give suggestions for improving it. Version 3.0 includes the usage of multiple row testing matrix input.

Comment only
04 Apr 2012 Anand Mishra

Anand Mishra (view profile)

Sorry for late reply, but I was little bit busy.
You can use '|' instead of '||' or '&' instead of '&&' in function,hope this will work for your application. You can refer Operators and Flow Control in Matlab for more information.
Thank You for taking help.

Comment only
04 Apr 2012 Anand Mishra

Anand Mishra (view profile)

@Gomathi C,
Sorry for late reply, but I was little bit busy.
By the way you don't have to worry about using this function for two class SVM. You can not use this function for two classes, because their is an inbuilt MATLAB function for two classes classification present in MATLAB IDE. I created this function to extend the facilities of inbuilt MATLAB function.
I am not able to understand your output of GLCM but you can look image associated with my file in MATLAB file exchange for example of using my SVM function.
If you still find any problem in using your inputs for your program then please clarify me whole parameters you are using for GLCM like autoc, contr etc. So that I am able to tell you more clearly.
Hope this will help you.
Thanks for using my function.

Comment only
22 Mar 2012 Gomathi C

I actually obtained the texture features using GLCM. My output for that was

stats =

autoc: [1.857855266614132e+000 1.857955341199538e+000]
contr: [5.103143332457753e-002 5.030548650257343e-002]
corrm: [9.512661919561399e-001 9.519459060378332e-001]
corrp: [9.512661919561385e-001 9.519459060378338e-001]
cprom: [7.885631654779597e+001 7.905268525471267e+001]

Now how should I give this as an input to the SVM program. Kindly guide me.

Comment only
16 Feb 2012 pallavi

when I run the program , I fet an error:
??? Operands to the || and && operators must be convertible to logical scalar values.
how to solve it?

Comment only
14 Oct 2011 Anand Mishra

Anand Mishra (view profile)

Hi, you correctly spotted the algorithm behind this file.
Now take an example,
T=[1 2 3 4;2 3 4 5;3 4 5 6;4 5 6 7]
C=[1 2 3 4]
tst=[3 4 5 6]
then run the function and then debug it step by step to understand algorithm.
It becomes clearer how the class is calculated.
Thanks for your interest.

Comment only
13 Oct 2011 guo

guo (view profile)

Hi again. I wonder how you solve an multi-class problem. It is likely that you decompose an M-class problem into a series of two-class problems using one-against-all implementation, right?

Comment only
13 Oct 2011 guo

guo (view profile)

Helloļ¼Could you please give a detailed example for input parameter specification?

11 Oct 2011 Anand Mishra

Anand Mishra (view profile)

Dear Aziz, svmclassify never makes 'classes' variable as a row vector of 0 and 1. The 'classes' can used only for checking loop condition i.e.,whether loop is to be terminate or not (check in while condition).
Variables 'newClass' and 'c1' can takes form of vector(0 and 1) and changing their sizes in each iteration.

I am using operands || and && because I am working on C Programming also so I am habitual of using this instead of logical scalar values.

May this solve out your confusions. Read Comments on file thoroughly. Thanks for taking help.
Regards...Anand Mishra.

Comment only
10 Oct 2011 Aziz

Aziz (view profile)

This is great but I wonder what purpose the 'classes' variable serve? At first it looks like an indicator to keep looping if the 'classes' variable still not equal 1 (line 32, line 65-67) but then you use it as result from svmclassify (line 39), which will make it a row vector of 0 and 1. Hence it couldn't be used in loop indicators. It would make this appear

??? Operands to the || and && operators must be convertible to logical scalar values.

Am I missing something?

Comment only
14 Oct 2011 1.1

This is Version 2.0 of Original Function.

04 Apr 2012 1.2

This is the update version 3.0 of original created Multi-Class SVM function by me.

04 Apr 2012 1.3

This is version 3.0 of original function.

04 Mar 2015 1.4

Now the function is available in toolbox also.

Contact us