Creating a table with every possible combination of rows from other tables

21 views (last 30 days)
Hello,
I currently have multiple tables with different sets of information in. I am wanting to create one large table that includes all the possible combinations of all of these sets of data in. For example, it would create a table by taking the first row of the first table and then iterating through all of the remaining tables until all possible combinations had been displayed in a table. Is there a way to do this?
Many thanks for your help,
Josh
  2 Comments
the cyclist
the cyclist on 20 Mar 2021
What you want to do is not clear to me. Showing a small example of the input and output would be very helpful in understanding what you mean.
Josh Beaumont
Josh Beaumont on 23 Mar 2021
Sorry, I haven't explained it very well.
For example, if there were multiple tables with different students names (A,B,C,...) and the scores they got in different tests (1,2,3,...) would be the input. The desired output would be a table showing all of the possible combinations of students and the test they take and the addition of all of their scores to see which combination of students/tests would give the highest/lowest score . For example, one row would display which student was selected for each test and the combined score. The next row would then display the next possible combination.
I hope that this is a bit clearer than my original question.
Thanks for your help,
Josh

Sign in to comment.

Answers (1)

Gargi Patil
Gargi Patil on 24 Mar 2021
If my understanding is right, you have multiple tables with each row corresponding to the various test scores for a student. The desired output should present an exhaustive combination such that each combination has all the tests listed and a particular student is chosen for each test.
A possible workaround to the problem could be to display the scores of the students in the output for each test instead of the student names. You can then map the scores to the corresponding students.
The above can be achieved by extracting the scores of all the students for a particular test as a vector. You can then run the following code:
%Let test1, test2, test3, test4 be vectors containing test scores
[W, X, Y, Z] = ndgrid( test1, test2, test3, test4);
out = [W(:), X(:), Y(:), Z(:)];
Each row of the output matrix ‘out’ will contain a possible combination with the score of the student displayed for the test. The combined score can be calculated summing across the values of the row.
You can refer to the further link for more information:

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!