I can't seem to work out why I can't perform a 5 grouped Kruskal-Wallis test on unequal sample sizes in matlab. Any thoughts how to do this? Also as a post-hoc analyis using Wilcoxon Rank sum is this individually comparing the groups?



I observed the length of 5 types of nursing care however now have 5 groups of differing sample sizes, just because type 1 care took place more often.

So when I run [P,ANOVATAB,STATS]=kruskalwallis([rand(10,1) rand(30,1)])

I get : Error using horzcat CAT arguments dimensions are not consistent.

Why do unequal sample sizes matter and what should I do instead?

  • $\begingroup$ What specifically are you doing and what is going wrong? $\endgroup$
    – whuber
    Mar 29, 2013 at 23:41
  • $\begingroup$ @whuber I am wanting to find out if the length of 5 different types of care are statistically significantly different between themselves. $\endgroup$
    – HCAI
    Mar 30, 2013 at 0:13
  • 1
    $\begingroup$ Equal sample sizes are not required in Kruskal Wallis. This looks as if it might be more of a matlab issue (either an issue with the implementation or with your usage of it) than a statistical one. Should this post be flagged to be moved to SO? $\endgroup$
    – Glen_b
    Mar 30, 2013 at 0:22
  • $\begingroup$ @Glen_b Can do. Do you know how the equivalent would be run in R? I have the data in files called: act_1.txt act_2.txt etc. $\endgroup$
    – HCAI
    Mar 30, 2013 at 0:27
  • $\begingroup$ stat.ethz.ch/R-manual/R-patched/library/stats/html/… $\endgroup$ Mar 30, 2013 at 0:29

1 Answer 1


It looks to me from reading the help on Matlab's Kruskal Wallis test as if it would be easy to stack up your different responses and have an indicator for group, using the kruskalwallis(x,group) syntax.

You ask about doing it in R in comments.

Here's the first example from the R help on kruskal.test, done first using three groups placed into a list, and the second way by stacking them and constructing a group variable:

> x <- c(2.9, 3.0, 2.5, 2.6, 3.2) # normal subjects
> y <- c(3.8, 2.7, 4.0, 2.4)      # with obstructive airway disease
> z <- c(2.8, 3.4, 3.7, 2.2, 2.0) # with asbestosis
> kruskal.test(list(x, y, z))

    Kruskal-Wallis rank sum test

data:  list(x, y, z) 
Kruskal-Wallis chi-squared = 0.7714, df = 2, p-value = 0.68

> ## Equivalently,
> x <- c(x, y, z)
> g <- factor(rep(1:3, c(5, 4, 5)),
+             labels = c("Normal subjects",
+                        "Subjects with obstructive airway disease",
+                        "Subjects with asbestosis"))
> kruskal.test(x, g)

    Kruskal-Wallis rank sum test

data:  x and g 
Kruskal-Wallis chi-squared = 0.7714, df = 2, p-value = 0.68

Then there's the formula interface:

> kruskal.test(x~g)

    Kruskal-Wallis rank sum test

data:  x by g 
Kruskal-Wallis chi-squared = 0.7714, df = 2, p-value = 0.68

Now for random data without names (sample sizes 30 and 10):

> kruskal.test(list(rgamma(30,4,.1),rgamma(10,4,.2)))

    Kruskal-Wallis rank sum test

data:  list(rgamma(30, 4, 0.1), rgamma(10, 4, 0.2)) 
Kruskal-Wallis chi-squared = 4.3795, df = 1, p-value = 0.03637

Now for a whole bunch of different-sized groups:

> kruskal.test(list(rgamma(30,4,.1),rgamma(10,4,.2),rgamma(5,4,.3),

    Kruskal-Wallis rank sum test

data:  list(rgamma(30, 4, 0.1), rgamma(10, 4, 0.2), rgamma(5, 4, 0.3), 
      rgamma(6, 4, 0.25), rgamma(8, 4, 0.28)) 
Kruskal-Wallis chi-squared = 16.8088, df = 4, p-value = 0.002105


I'm sorry I seem to have missed the question inside this comment before: "How does R deal with empty values?" -- you seem to be assuming the values will be stored in a rectangular array and since the groups are of different size, there will be missing values. Do I have that right?

Well, two issues:

1) R has the data value NA to represent missing values. It is of any type (or rather, there's one of each type).


x <- c(2.9, 3.0, 2.5, 2.6, 3.2) 
y <- c(3.8, 2.7, 4.0, NA, 2.4)     


       x   y
[1,] 2.9 3.8
[2,] 3.0 2.7
[3,] 2.5 4.0
[4,] 2.6  NA
[5,] 3.2 2.4

2) R has data structures suited to non-rectangular data (like lists) that will allow you to avoid this problem in any case. For example, depending on what you're trying to do, you could read each row into a vector and put those vectors into a list; the differences in length wouldn't matter. They could then be handled with any of the above methods.


R can read in a variety of formats and a variety of ways of indicating missing values, or it can do formatted reads (and so identify them that way), so your ascii files should present no problems. It's relatively straightforward.

Or, if you already have your data in matlab, the R.matlab package should help.

Personally I'd just go with reading the data in. If you have something more complex than the usual sort of thing (covered in the help on read.table and variants or scan) you can always look at the relevant manual.

If you can show the kind of format of your files I may be able to give more detailed help.

Note that there are various documents for matlab or octave users to help them with R.

  • $\begingroup$ Thank you for describing it in thorough detail. I have the data in act_1.txt act_2.txt ect. These are non-rectangular arrays of arbitrary length both rows and columns. But before anything I need to perform a of count number of entries in each row. eg row 1: 2 3 4 2 3 5 6. row 2: may have more or less entries. How does R deal with empty values? Perhaps easiest for me to do all that in matlab then save and import the data in R, though I'm sure it's possible to do. $\endgroup$
    – HCAI
    Mar 30, 2013 at 9:12
  • 1
    $\begingroup$ please note my answer has been changed to discuss the question contained within the above comment (which question I don't think I saw before) $\endgroup$
    – Glen_b
    Apr 8, 2013 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.