How to create a matrix out of all the possible combinations of a vector
Show older comments
Hi !
I want to fill a vector with specifice numbers of 1's and -1's, and the rest are zeros. And get all possible combinations in each row of a matrix.
For example, if I have a vector of length 12 and I want to have 5 elements = +1 and 4 elements = -1
Then, the results should be like:
M=[1 1 1 1 1 -1 -1 -1 -1 0 0 0; 0 0 0 1 1 1 1 1 -1 -1 -1 -1; -1 1 1 1 1 1 -1 -1 -1 0 0 0; .....]
and every other distinct combination of the three numbers.
Accepted Answer
More Answers (1)
Rik
on 23 Jul 2019
1 vote
https://www.mathworks.com/help/matlab/ref/perms.html
2 Comments
Nora Khaled
on 23 Jul 2019
Edited: Nora Khaled
on 23 Jul 2019
Rik
on 23 Jul 2019
All combinations will result in a huge matrix if you have a large number of elements. As the documentation warns: this is only practical for a small number of elements. This is a fundamental problem that you need to solve separately.
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!