MATLAB Answers

boxplot with vectors of different lengths

423 views (last 30 days)
Vahid on 1 Feb 2013
Commented: Joey Porter on 21 Apr 2020
Hi MATLAB folks,
I am wondering how I can boxplot two column matrices with different lengths, e.g.
how I can do
Is there any solution to that?
Many thanks in advance, -V


Sign in to comment.

Accepted Answer

Shashank Prasanna
Shashank Prasanna on 1 Feb 2013
BOXPLOT works with grouping variables, so you can manually append all of your data together and then create a grouping variable that lets boxplot know which belongs to first and which for second. Take a look at the example below:
>> c_1=rand(1,20);
>> c_2=rand(1,100);
>> C = [c_1 c_2];
>> grp = [zeros(1,20),ones(1,100)];
>> boxplot(C,grp)


Show 1 older comment
Gabriel Earley
Gabriel Earley on 14 Nov 2015
To do multiple box plots you just multiply your ones matrix by which ever box your on plus 1. So if I was doing the 4th box of 50 data points it would be 5*one(1,50)
Adam Danz
Adam Danz on 22 Nov 2019
Here's another version of Shashank Prasanna's answer that makes it easy to add more groups of data. The only change needed is in the 3rd line below: simply add your row or column variables to C as shown. That's the only change needed.
c_1=rand(1,20); % Generate group 1
c_2=rand(1,100); % Generate group 2
C = {c_1(:); c_2(:)}; % <--- Just vertically stack all of your groups here
grp = cell2mat(arrayfun(@(i){i*ones(numel(C{i}),1)},(1:numel(C))'));

Sign in to comment.

More Answers (2)

Matt Raum
Matt Raum on 23 Mar 2017
I ran into the same issue -- here's a quick re-wrapping of boxplot that can be called on a cell array containing vectors of variable size:
boxplot2 automatically generates the necessary grouping array. All you need to pass to it is a cell array of the vectors you want box plotted.

  1 Comment

Alexandre Soares da Silva
Alexandre Soares da Silva on 28 Apr 2017
Thank you so much for that wrapper. Saved my statistics homework ;)

Sign in to comment.

Joey Porter
Joey Porter on 21 Apr 2020
This is my first feedback so please forgive if I've misunderstood but I solved this issue with a simpler explanation (at least for me).
boxplot plots each columns as a separate variable (box) as long as the lengths of each column are the same.
I simply created a NaN matrix with number of rows equal to the length of my longest variable, then populated with my variable data. The NaNs are not plotted on the box plot but allows variables of unequal length to be plotted. Also to add more variables, simply increase the number of columns in your NaN array.
I hope this helps anyone returning to this thread :)


Adam Danz
Adam Danz on 21 Apr 2020
That's another good approach.
You can also use padarray() to pad the cell elements with NaN values.
% Create 1x3 cell array of vectors with different lengths
C = {rand(20,1), rand(15,1), rand(200,1)};
% Pad each vector with NaN values to equate lengths
maxNumEl = max(cellfun(@numel,C));
Cpad = cellfun(@(x){padarray(x(:),[maxNumEl-numel(x),0],NaN,'post')}, C);
% Convert cell array to matrix and run boxplot
Cmat = cell2mat(Cpad);
Joey Porter
Joey Porter on 21 Apr 2020
Thanks Adam.
I didn't know there was a function for padding. I'll use this next time!

Sign in to comment.