MATLAB: Show All Possible Combination and Max. Value

combinationmatrixmax value

I have 6 different matrix (A,B,C,D,E,F).
A = [
Row1 Row2 Row3 Row4
30 30A 9.2588 0.2059
60 60A 18.5053 0.4054
90 90A 27.7393 0.5983
120 120A 36.961 0.7847
150 150A 46.1702 0.9645
180 180A 55.3671 1.1379
]
B = [
Row1 Row2 Row3 Row4
30 30B 9.2588 0.2059
60 60B 18.5053 0.4054
90 90B 27.7393 0.5983
120 120B 36.961 0.7847
150 150B 46.1702 0.9645
180 180B 55.3671 1.1379
]
C = [
Row1 Row2 Row3 Row4
30 30C 6.4351 0.314
60 60C 12.8844 0.6176
90 90C 19.3479 0.911
120 120C 25.8256 1.1941
150 150C 32.3175 1.4669
180 180C 38.8235 1.7294
]
D = [
Row1 Row2 Row3 Row4
30 30D 9.8998 0.2259
60 60D 19.6938 0.4449
90 90D 29.3821 0.657
120 120D 38.9645 0.8622
150 150D 48.4412 1.0606
180 180D 57.8121 1.2521
]
E = [
Row1 Row2 Row3 Row4
30 30E 6.4351 0.314
60 60E 12.8844 0.6176
90 90E 19.3479 0.911
120 120E 25.8256 1.1941
150 150E 32.3175 1.4669
180 180E 38.8235 1.7294
]
F = [
Row1 Row2 Row3 Row4
30 30F 9.8998 0.2259
60 60F 19.6938 0.4449
90 90F 29.3821 0.657
120 120F 38.9645 0.8622
150 150F 48.4412 1.0606
180 180F 57.8121 1.2521
]
I am looking for combination of A+B, A+C, A+D, A+E, A+F, B+C, ...., A+B+C, A+B+D, ... A+B+C+D, A+B+C+E, ... A+B+C+D+E+F.
For example, the result of A+B
A+B = [
Row1 Row2 Row3 Row4
30+30 30A+30B 9.2588+9.2588 0.2059+0.2059
30+60 30A+60B 9.2588+18.5053 0.2059+0.4054
...
60+30 60A+30B 18.5053+9.2588 0.4054+0.4054
180+180 180A+180B 55.3671+55.3671 1.1379+1.1379
]
= [
Row1 Row2 Row3 Row4
60 30A+30B 18.5176 0.4118
90 30A+60B 27.7641 0.6113
...
90 60A+30B 27.7641 0.6113
360 180A+180B 110.7342 2.27958
]
Finally, I hope to find the max value of row 4 when the value in row 1 is equal in all combination, such as
Row 1 column1 (30) in A compare with Row 1 column 1 (30) in C,
Max value [
Row1 Row2 Row3 Row4
30 30C 6.4351 0.314
]
Thank you for your help!

Best Answer

Here is my approach to the first step (getting the sum of all combinations)
% step 0, create a small fake data set
A = randi(4,4,4) ; B = 10+randi(4,size(A)) ; C = 100+randi(4,size(A)) ;
% 1. put all matrices in a big 3D array
CM = cat(3,A,B,C)
% 2. make a list of all the combinations of 2 or more
acidx = nchoose(1:size(CM,3))
acidx(cellfun(@(c) numel(c)<2,acidx)) = []
% 3. sum all combinations
R = cellfun(@(c) sum(CM(:,:,c),3), acidx,'un',0)
The function NCHOOSE can be downloaded from the File Exchange: https://uk.mathworks.com/matlabcentral/fileexchange/20011-nchoose