필터 지우기
필터 지우기

Dynamically generating a set of combinations for elements of a vector

조회 수: 1 (최근 30일)
Mark Thompson
Mark Thompson 2016년 10월 18일
댓글: Mark Thompson 2016년 10월 19일
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