Creating a row vector of combinations?
5 views (last 30 days)
Ali Almakhmari on 24 Jan 2023
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]];
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)
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
X = combinator(numel(V),4,'p','r');
M = V(X)