Dynamically generating a set of combinations for elements of a vector

1 次查看(过去 30 天)
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!!

回答(1 个)

Walter Roberson
Walter Roberson 2016-10-18
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 个评论
Mark Thompson
Mark Thompson 2016-10-19
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by