All possible combinations of variables

11 次查看(过去 30 天)
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 个评论
Andrew Reibold
Andrew Reibold 2014-8-5
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 2014-8-5
No, I need all of the combinations output. Could I email you in order to be more explicit?
Thank you!

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2014-8-5
编辑:Star Strider 2014-8-5
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 个评论
Josep
Josep 2014-8-13
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 2014-8-13
编辑:Star Strider 2014-8-13
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by