All possible combinations of variables

32 views (last 30 days)
Hey everybody,
I have a system with x variables and I want to obtain by using MATLAB a combination of all of them in all the possible groups. Let me introduce you an example:
Lets suppose I have 6 variables for example(1,2,3,4,5,6), it means there will be 63 combinations without repeating any. My aim is to achieve all the combinations like this:
[1] [2] [3] [4] [5] [6]
[1,2] [1,3] [1,4] [1,5] [1,6] [2,3] [2,4] ...
[1,2,3] [1,2,4] [1,2,5] [1,2,6] [1,3,4] [1,3,5] [1,3,6] [1,4,5] [1,4,6] [1,5,6] [2,3,4] ....
and the same for groups of 4, 5 and 6 variables.
Does anybody know how to do it for X number of variables? I need to do this to compute an statistics equation in order to detect errors in the measured values of the system.
Thank you for your attention!
  2 Comments
Andrew Reibold
Andrew Reibold on 5 Aug 2014
Do you just need to know 'how many' combinations there are, or do you need all of the combinations output. If you need them output, how should they be displayed or stored?
Josep
Josep on 5 Aug 2014
No, I need all of the combinations output. Could I email you in order to be more explicit?
Thank you!

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 5 Aug 2014
Edited: Star Strider on 5 Aug 2014
If you have the Statistics Toolbox, use the combnk function:
v = 1:6;
c2 = combnk(v,2)
c3 = combnk(v,3)
... and so on for 4, 5, and 6.
  24 Comments
Josep
Josep on 13 Aug 2014
I have done this:
function combo = combs(6,2)
combinator(n,k,'c'); % Combinations without repetition
combo = ans;
end
And I still have an error message:
>> combs(6, 2) Error: File: combs.m Line: 2 Column: 24 Unexpected MATLAB expression.
Star Strider
Star Strider on 13 Aug 2014
Edited: Star Strider on 13 Aug 2014
That’s strange. Line 2 is the combinator call, and column 24 is in the comment!
I’ll download combinator in a few minutes to see if I can replicate your error.
Later that same day ...
I downloaded combinator, saved it to my user directory, and ran this code:
combinator(6,2,'c'); % Combinations without repetition
combo = ans;
without problems, returning combo as a (15x2) double matrix. I have no idea what may be the problem with your code, or perhaps with your MATLAB installation.
I still suggest you run this command from the Command Window:
which combinator -all
and copy the results to a comment to this thread.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!