I'm having trouble here turning a vector of vectors with different lengths into a matrix with all possible combinations. For example, I am wanting the below
vector <vector <double>> matrix1{{10, 20, 30}, {1}, {2, 3}, {1}};
to turn into
{10, 1, 2, 1}
{10, 1, 3, 1}
{20, 1, 2, 1}
{20, 1, 3, 1}
{30, 1, 2, 1}
{30, 1, 3, 1}
I've come up with the below, which gives me some of the above, but I am obviously missing the ones where c = 3 and a = 20, c = 3 and a = 30.
vector <vector <double>> TransformMatrix(vector<vector<double>> matrix)
{
vector <vector <double>> fullmatrix;
for (int a = 0; a < matrix[0].size(); a++)
{
vector <double> v1;
v1.push_back(matrix[0][a]);
v1.push_back(matrix[1][0]);
v1.push_back(matrix[2][0]);
v1.push_back(matrix[3][0]);
fullmatrix.push_back(v1);
}
for (int b = 1; b < matrix[1].size(); b++)
{
vector <double> v1;
v1.push_back(matrix[0][0]);
v1.push_back(matrix[1][b]);
v1.push_back(matrix[2][0]);
v1.push_back(matrix[3][0]);
fullmatrix.push_back(v1);
}
....
}
Does anyone have any helpful hints / tips here?
What you're trying to do here is obtain the inputs to an n-dimensional Cartesian product. The conventional solution is to write an iterative and recursive function:
Also note that it's excessive and probably unnecessary to store all results in
out. You might be able to just process thestackwhenever needed instead of storing all possible stacks in a vector first.See also How can I create cartesian product of vector of vectors? for similar solutions.