You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Creating a row vector of combinations?
1 view (last 30 days)
Show older comments
I have a row vector that 1 by 4. And I know that the minimum and maximum values of the elements in this vector to be -15 and 15. How can I make a matrix that is N by 4 that contains all possible combinations of ALL values?
Let me given an example: min = -15, max = 15, so the result of the code should be a matrix that is N by 4 that will look something like this
A = [[-15,0,0,0];[-14,0,0,0];[-13,0,0,0];[-12,0,0,0];[-11,0,0,0];[-10,0,0,0];[-9,0,0,0];....and so on until the final value vector to be[15,15,15,15]];
Accepted Answer
Davide Masiello
on 24 Jan 2023
You could use nchoosek. The example below is from -3 to 3 for memory issues (you'll generate a humongous matrix). Just substitute -15:15 and the trick is done.
A = nchoosek(-3:3,4)
9 Comments
Ali Almakhmari
on 24 Jan 2023
I love you
Davide Masiello
on 24 Jan 2023
XD my pleasure XD
Dyuman Joshi
on 24 Jan 2023
This, however, will not generate permutations with repetitions as OP wants.
[-3 0 0 0], [-2 0 0 0], [3 3 3 3] etc will not be created.
Ali Almakhmari
on 24 Jan 2023
Interesting. Any suggestions on how to incorporate them?
Dyuman Joshi
on 24 Jan 2023
As of now, No. I am still thinking. I'll update you if and when I get an idea.
Davide Masiello
on 25 Jan 2023
Edited: Davide Masiello
on 25 Jan 2023
@Dyuman Joshi thanks for pointing that out, I had missed it completely.
I think I came up with a solution, albeit slightly convoluted.
It performs all combinations of integer vector that must verify a criteria on the sum.
I embedded this function in a loop to obtain the desired result.
The logic is as such.
2 - I establish the maximum sum achievable which is the maximum value times the number of columns of the output matrix (i.e., 3x4=12 in this case).
3 - Then, I loop for every combination leading to a sum of the row vector that goes from 0 to the maximum achievable sum and I concatenate the result to matrix A each time.
4 - Since this is done for positive integers, after the loop I concatenate -A with A to make it symmetric around zero.
5 - I perform a unique operation to remove the duplicated [0 0 0 0] row.
Please find the result attached below (full A matrix).
I still supect there might be a much easier way to do this.
maxN = 3;
ncol = 4;
A = [];
for sum = 0:maxN*ncol
a = allVL1(ncol,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 3 1
1 0 3 3
1 1 2 3
1 1 3 2
1 2 1 3
1 2 2 2
1 2 3 1
1 3 0 3
1 3 1 2
1 3 2 1
1 3 3 0
2 0 2 3
2 0 3 2
2 1 1 3
2 1 2 2
2 1 3 1
2 2 0 3
2 2 1 2
2 2 2 1
2 2 3 0
2 3 0 2
2 3 1 1
2 3 2 0
3 0 1 3
3 0 2 2
3 0 3 1
3 1 0 3
3 1 1 2
3 1 2 1
3 1 3 0
3 2 0 2
3 2 1 1
3 2 2 0
3 3 0 1
3 3 1 0
0 2 3 3
0 3 2 3
0 3 3 2
1 1 3 3
1 2 2 3
1 2 3 2
1 3 1 3
1 3 2 2
1 3 3 1
2 0 3 3
2 1 2 3
2 1 3 2
2 2 1 3
2 2 2 2
2 2 3 1
2 3 0 3
2 3 1 2
2 3 2 1
2 3 3 0
3 0 2 3
3 0 3 2
3 1 1 3
3 1 2 2
3 1 3 1
3 2 0 3
3 2 1 2
3 2 2 1
3 2 3 0
3 3 0 2
3 3 1 1
3 3 2 0
0 3 3 3
1 2 3 3
1 3 2 3
1 3 3 2
2 1 3 3
2 2 2 3
2 2 3 2
2 3 1 3
2 3 2 2
2 3 3 1
3 0 3 3
3 1 2 3
3 1 3 2
3 2 1 3
3 2 2 2
3 2 3 1
3 3 0 3
3 3 1 2
3 3 2 1
3 3 3 0
1 3 3 3
2 2 3 3
2 3 2 3
2 3 3 2
3 1 3 3
3 2 2 3
3 2 3 2
3 3 1 3
3 3 2 2
3 3 3 1
2 3 3 3
3 2 3 3
3 3 2 3
3 3 3 2
3 3 3 3 ];
Davide Masiello
on 25 Jan 2023
Further thoughts.
The solution in my previous comment does not include combinations in which positive and negative integers appear in the same row vector.
This could be obtained with the following hack
maxN = 3;
ncol = 4;
A = [];
for sum = 0:2*maxN*ncol
a = allVL1(ncol,sum);
a = a(all(a<=2*maxN,2),:);
A = [A;a];
end
A = A-maxN
I am not posting the entire matrix again, its 2401 x 4.
Here's a snippet of it where both negative and positive integers appear at the same time.
A(500:505,:)
ans =
-3 1 -1 0
-3 1 0 -1
-3 1 1 -2
-3 1 2 -3
-3 2 -3 1
-3 2 -2 0
Moreover, we can check if the upper and lower bounds are correct
max(A)
ans =
3 3 3 3
min(A)
ans =
-3 -3 -3 -3
Dyuman Joshi
on 25 Jan 2023
Nice work, Davide! I sometimes forget that FEx is an amazing resource as well
And the solution checks out, there should be 7^4 rows total for [-3 3]
7^4
ans = 2401
@Ali Almakhmari, you can obtain the desired solution for the intended range, but it would contain
fprintf('%d rows', 31^4)
923521 rows
so it would be difficult to go through all of them.
Ali Almakhmari
on 25 Jan 2023
Thank you all for your help. You have been amazing!
More Answers (1)
Stephen23
on 25 Jan 2023
One simple aprpoach is to download this FEX submission:
but you will need plenty of memory:
V = -3:3
V = 1×7
-3 -2 -1 0 1 2 3
X = combinator(numel(V),4,'p','r');
M = V(X)
M = 2401×4
-3 -3 -3 -3
-3 -3 -3 -2
-3 -3 -3 -1
-3 -3 -3 0
-3 -3 -3 1
-3 -3 -3 2
-3 -3 -3 3
-3 -3 -2 -3
-3 -3 -2 -2
-3 -3 -2 -1
7 Comments
Dyuman Joshi
on 25 Jan 2023
Does MATLAB Answers (and MATLAB Live Editor by extension) have the ability to directly run FEx functions/files?
Ali Almakhmari
on 25 Jan 2023
Thank you all for your help. You have been amazing!
Dyuman Joshi
on 25 Jan 2023
"You might be able to copy them automatically, if you know the URL."
What do you mean by copy them? Also, how did you run the function?
Davide Masiello
on 25 Jan 2023
I have the same question.
Stephen23
on 26 Jan 2023
Edited: Stephen23
on 26 Jan 2023
"What do you mean by copy them?"
Perhaps COPYFILE or one of the WEB* or URL* functions lets you copy from the FEX location to the Answers environment, from whence you could run it. Go and read the documentation and do some experiments. I can't help you with this, I have never tried.
"Also, how did you run the function?"
I downloaded it from FEX, uploaded it here, ran the code, then (out of respect for the submitter's license conditions) deleted the function before submitting my answer. You can delete uploaded files by clicking on the little red "x":

Dyuman Joshi
on 27 Jan 2023
Okay, I'll try it. Thanks for replying.
"I downloaded it from FEX, uploaded it here, ran the code, then (out of respect for the submitter's license conditions) deleted the function before submitting my answer."
Fair enough.
See Also
Categories
Find more on Entering Commands 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)