Code covered by the BSD License  

Highlights from
Wilcoxon test


3.5 | 2 ratings Rate this file 28 Downloads (last 30 days) File Size: 3.66 KB File ID: #12702
image thumbnail

Wilcoxon test



19 Oct 2006 (Updated )

non parametric Wilcoxon test for two paired samples

| Watch this File

File Information

This file execute the non parametric Wilcoxon test to evaluate the difference between paired (dependent) samples. If the number of difference is less than 15, the algorithm calculate the exact ranks distribution; else it uses a normal distribution approximation.
Now, the MatLab function SIGNRANK returns the same p-value. Anyway, this Wilcoxon function gives a more detailed output (that is necessary for publications...)

You can visit my homepage
My profile on XING
My profile on LinkedIN

Required Products Statistics Toolbox
MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (24)
21 Jan 2013 Giuseppe Cardillo

if you type: wilcoxon it will works showing the example.

in the other cases you have to write:
x1=[n1 n2 n3 n4];
x2=[m1 m2 m3 m4 m5...mi];

where are the values of the first vector and m2...mi are the values of the second vector.

21 Jan 2013 Madi

Thanks for your reply... actually I'm using a sample that you given in the example.. the error is:

WILCOXON requires vector rather than matrix data'

Then when I change to 'elseif nu<=2'

The result I get are:
w= 325.0000
mw = 0
sW = 73.1608
zW = 4.4354
p-value (2-tailed) = 1.000000000000000e+000

almost same, only p-value is different

21 Jan 2013 Giuseppe Cardillo

Madi, I wrote Wilcoxon to manage two vectors and not a matrix.
nu is the number of inputs that you give to the function. If you change nu...I don't know what are you doing...

19 Jan 2013 Madi

sorry typo error:

elseif nu<=2

19 Jan 2013 Madi

From my previous comment,'WILCOXON requires vector rather than matrix data'

then I change the code to:

'elseif nu<=2' and now OK.. it is true?

19 Jan 2013 Madi

Hi, when I follow the example, the this error coming out:

'WILCOXON requires vector rather than matrix data'

What should I do?

06 Dec 2012 Giuseppe Cardillo

No it is not normal: ther is a bug on row 152.
The correct command is:
p=min([1 2*normcdf(zW)]); %p-value

I've just upload the correct version
Thank you

05 Dec 2012 JR King

Is it normal that if I do:
stats = wilcoxon(rand(1,100),rand(1,100))
it return a p-value of 0?

09 May 2011 Shabnam

Thank you very much for the references. Just for the future reference, I would like to emphasize the following points:

1. According to Stanton Glantz's book (Chapter 10), "When there are tied ranks, and we use the normal distribution to compute the
P value, sigma_w needs to be reduced by a factor that depends on the number of ties". You have used this formula in your function.

2. The correction for continuity has been implemented based on the second reference mentioned above (

Please correct any false statement mentioned above.

Thank you very much for your help,

09 May 2011 Giuseppe Cardillo

A. So, do you mean that "0.5" in Line 123 (zW=(abs(W)-0.5)/sW) is the correction for continuity; however the real mean of the distribution of W is 0?
R. Yes 0.5 is the Yates'es Correction for continuity. If you have a great number of subjects W distribution can be approximated with a normal distribution that has mean=0 and a computed standard deviation.

A. I read a little about the concept of "continuity correction"; however, it seems that those methods including Yates' Correction have not been designed for the normal distribution. I really appreciate if you send me your reference on this particular case (i.e. for the normal distribution)
R. Yates'es correction can be applied in every case in which a discrete distribution is approximated by a continue distribution. If you read well you can find this in Stanton Glantz book, chapter 10. Anyway another (and clear) reference is


09 May 2011 Shabnam

Thanks a lot for the prompt response.

So, do you mean that "0.5" in Line 123 (zW=(abs(W)-0.5)/sW) is the correction for continuity; however the real mean of the distribution of W is 0?

I read a little about the concept of "continuity correction"; however, it seems that those methods including Yates' Correction have not been designed for the normal distribution. I really appreciate if you send me your reference on this particular case (i.e. for the normal distribution)

Thank you very much,

09 May 2011 Giuseppe Cardillo

Dear Shabnam,
first of all you compute W from your data. If the null hypothesisi is true, the signed rank will distribute equally between groups and so W->0. To check if W is not different from 0 you can use the normal approximation. A normal distribution has two parameters: a mean, that in this case is set to 0, and a standard deviation that is computed. Another one point: normal istribution is a continue distribution, but W is discrete. To check W against this normal distribution, it must be normalized: you will compute z=|W|/std. To include the Yates'es correction for continuity: (|W|-0.5)/std.
As you can see zW is not 0.5

08 May 2011 Shabnam

According to Chapter 10 of the book by Stanton A. Glantz (Primer of Biostatistics) and as you mentioned above, mu_w=0 (mu_w is the mean of the distribution of W). My question is why line 123 of your code shows that mean = 0.5 :


09 Oct 2010 Giuseppe Cardillo

Yes for all questions

08 Oct 2010 Jorge Amaral

Is this test is also called Wilcoxon Signed Rank Test and also Wilcoxon matched pairs?

If the p-value is less than 0.05 can I say that there is a difference between X1 and X2? Like in the example?

07 Mar 2008 Antonio Trujillo-Ortiz

Only to correct and clarify my previous comment, and to recall to all that, with respect to the non parametric tests between two samples, two classes exist: (1) Independent, that can be both of the same size or not, being been able to utilize the Mann-Whitney’s test or the Wilcoxon ranks test; and (2) Dependent, that necessarily them should be of the same size, since is the same sample observed in two different circumstances or times, where applies the Wilcoxon signs and ranks test.

Also, I should clarify that in the personal thing I do not use the term unpaired to assign independent samples, since unpaired is synonym of unbalanced; neither paired for assign dependent, since paired is synonym of balanced. This by the fundamental reason that the concepts of independent or dependent arise for the itself nature of the data, and not because they are unpaired or paired.

The m-file here developed by Giuseppe it is a Wilcoxon’s test for two dependent samples for it is correct his comment that the data vectors must have the same length, and, if it is not, delete those uncoupled data. There isn't any other solutions.

So, my previous review comment is obsolete and should be ignored.


Prof. Antonio Trujillo-Ortiz

05 Mar 2008 Antonio Trujillo-Ortiz

Hi Giuseppe,

You are wrong. According to the non-parametric statistics theoretic fundamentals, there are a two Wilcoxon tests:(1)Wilcoxon Rank Test for two unpaired samples, and(2) Wilcoxon Sign-Rank Test for two paired samples. You must to review them.


Prof. Antonio Trujillo-Ortiz

12 Jun 2007 Giuseppe Cardillo

The Wilcoxon's test is a paired test. Infact it test if the differences observed in a group after an event (in exemplum drug subministration) are caused by the event or by chance. If your vectors differ in length you must delete the uncoupled data. I think there isn't any other solutions.

08 Jun 2007 James Cai

If my vectors, X and Y, are different in length, can I use wilcoxon(X,Y)? If not, what's the solution? Thanks.

25 Oct 2006 Giuseppe Cardillo

You have right. I fixed the bug and uploaded the new file.

24 Oct 2006 Brahim HAMADICHAREF

Good but code need line 57

if min(rowx, colx) > 1 || min(rowy,coly) > 1,

to be replaced with

if((min(rowx1, colx1) > 1) || (min(rowx2,colx2) > 1))


20 Oct 2006 Giuseppe Cardillo

ERRATA CORRIGE: the TIEADJ value computed by TIEDRANK is quite different from correction reported in several books. The same for z-value because W is approximated with a normal distribution with mean=0 and In this algorithm the tie correction is computed as reported in Stanton A. Glantz book and the normal distribution has mean=0 and a calculated (depending on tie correction).

20 Oct 2006 Giuseppe Cardillo

RANKSUM and SIGNRANK don't compute the correction for the ties.

20 Oct 2006 Jos x@y.z

As this requires the statistics toolbox, the function RANKSUM and SIGNRANK are available to the users. What does this function adds to those two?

23 Oct 2006

m-lint and errors handling improvement

23 Oct 2006

fix in errors messages

23 Oct 2006


25 Oct 2006

fix bug

30 Oct 2006

Error handling and calculation improvements

10 Mar 2008

Change in DEscription according to Antonio Trujillo-Ortiz comment

12 Nov 2008

Changes in help section

23 Mar 2009

Little correction in help section to allow a correct copy and paste of the example. Correction in exitus subroutine.

06 Nov 2009

bug correction

11 Nov 2009

The output and the error handling were changed

13 Nov 2009

the STATS struct nargout was added

16 Nov 2009

Changes in help and description sections

23 Dec 2009

Changes in description

23 Mar 2010

I added the plts flag to choose to show the plots

12 Jan 2012

the Hodges-Lehmann estimator of median of differences was added

06 Dec 2012

bug correction in normal approximation p-value

Contact us