# All combinations from vectors 8

Posted by **Doug Hull**,

My colleague walked into my office with a MATLAB question, a regular pasttime for us here at the MathWorks. He wanted to take every combination of one value from each of three distinct vectors. Now, this could be done easily with some nested for loops, but that really does violate the spirit in which such challenges are issued. I did not know of a function in MATLAB that did that, but I figured someone had invented that wheel before, so off to the File Exchange. A few minutes later I remembered the difference between combinations and permutations and had found two great new picks for this week.

Permutations: A great little file, with published documentation, that will find all permutations with lots of options.

Combinations: Another great little utility that answered my colleague’s challenge. It was nice to see that the author, Jos, followed up on comments from reviewers and made some good enhancements to the file.

## 8 CommentsOldest to Newest

**1**of 8

Doug,

I’ve been struggling with a matlab problem of combinations and permutations for a while and came across your post here. I thought you might be able to help me out.

Let’s say I have a vector say A =[1 2 3 4] and want to find every way I can choose all the elements of A to fit in some number of bins (let’s say 3). Bin 1 could have (1, 2, 3), Bin 2 could have (4) or (0) and Bin 3 could have (4) or (0) as well. The problem isn’t quite solved by “nchoosek” because each bin could have more or less elements than the previous bin and the element choice for each bin depends on the elements not already chosen. I know that the number of possible combinations is # of elements in A^#of bins (in this case 81), because I solved it out by hand. I am looking for an easier way! I’m sorry if this sounds confusing!

If you can point me in the right direction or offer me any help I would greatly appreciate it!

Thank you,

Amanda Palazzo

**2**of 8

I think you need to look at the problem as such. Each element of the vector can reside in exactly one of three bins.

3 * 3 * 3 * 3 or numBins ^ numElements

-Doug

**3**of 8

Thank you very much, I was just looking for an algorithm like this!

**4**of 8

I have tried to use perms.m for my problem but it didn’t work. Suppose that we have 4 objects, A, B, C, and D. And I would like to assign them to 3 unique labels/ groupings – 1,2 or 3. So all possible unrepetitve combinations will be:

1 1 2 3 (meaning A and B in group 1, C in group 2 and D in group 3)

1 2 1 3

1 2 3 1

2 1 1 3

2 1 3 1

2 3 1 1.

perms.m gave me the extras,

1 1 3 2, 1 3 1 2, 1 3 2 1, 3 1 1 2, 3 1 2 1 and 3 2 1 1 which I do not need. For example 1 1 3 2 still assign A ann B in group 1.

If you could point me in the right direction, I would greatly appreciate it.

Thank you

Marina

**5**of 8

@Marina,

What you are asking for is not permutation exactly. I am not sure that what you are asking for has a specific name in statistics, so you will have to write your own MATLAB code to solve this.

It should not be too hard. I am assuming none of the N groups that hold M objects is to be empty, and that every object is in a group. If so, you can calculate how many different ways to group things without perms.m

Doug

**6**of 8

Let NN=[2 1 2] (each element is number of combinations) and we need matrix of all combinations such as:

1 1 1

1 1 2

2 1 1

2 1 2

Here is solution:

for i=1:prod(NN) t=i-1; for j=length(PP):-1:1 M(i,j)=mod(t,NN(j))+1; t = fix(t/NN(j)); end end

**7**of 8

Yury, What is PP?

**8**of 8

Yes, Yuri what is pp? I do not know if this would work with my impdance matrix problem. There are 3 varibles, and each can have a value from 1 to 3, and all possible sets of 2 of this variables are: 11 22 33 12 13 21 31 23 32 All in all, 3*3= 9, which is easy to do manually. Now, doing the same as above, but now with 6 variables, each one can have value form 1 to 9, making a total of 9*9*9*9*9*9=531441. Has anybody got and idea, please? Zoran

## Recent Comments