Clear Filters
Clear Filters

Dynamically generating a set of combinations for elements of a vector

1 view (last 30 days)
Hi there,
I have to dynamically define a vector m = (m_x, m_y, m_z) where each of the 3 elements in m can take values between -Nm and Nm creating a set of (2Nm +1)^2 combinations. Can anyone help me define all combinations of the possible values of m for a given value of Nm?
Thank you!!

Answers (1)

Walter Roberson
Walter Roberson on 18 Oct 2016
The functions I show there are for 6 sets, but otherwise the code is generalized -- notice there that the first thing I do is toss the values to select from into a cell array, so you can follow on from after that step if your values are already in a cell array.
  1 Comment
Mark Thompson
Mark Thompson on 19 Oct 2016
Hi Walter,
I have managed to look at your method, and whilst technically it appears to be providing a "mesh" of combinations it doesn't present all of the combinations back in groups of 3 values (i.e. mx, my and mz).
I did learn that I have access to the combvec() function with my current MATLAB license so I have managed to implement this solution using the following:
Nm = 6;
mx = (-1*Nm):1:Nm;
my = (-1*Nm):1:Nm;
mz = (-1*Nm):1:Nm;
m = combvec(mx,my,mz)';
The resulting array (2197x3) m after executing this code contains 2197 rows of unique combinations of mx, my and mz, which is what I was originally after.
Thank you again for providing your method for consideration though.

Sign in to comment.

Categories

Find more on Matrices and Arrays 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!