Multiple combinations of a matrix

I have a matrix m, with 2 columns, M= [1, 2; 3, 4; 5, 6].
I need to the combinations of column one with its assocated column two entry.
For example:
x = [1, 2, 3, 4; 1, 2, 5, 6; 3, 4, 5, 6]

2 Comments

Do you want to be able to do this generally for any number of rows?
Yes, for any number of rows please.

Sign in to comment.

 Accepted Answer

Turlough Hughes
Turlough Hughes on 24 Apr 2020
Edited: Turlough Hughes on 25 Apr 2020
Try the following, it should work for any size of M.
function x = pairCombos(M)
numRows = size(M,1);
C = combnk(1:numRows,2); % Lists every pair combination of row indicies.
x = zeros(size(C,1),size(M,2)*2); % Preallocate space for variable x.
for i = 1:size(C,1)
x(i,:) = [M(C(i,1),:) M(C(i,2),:)]; % generate x as requested.
end
end
The final order depends the output from the combnk function. You might also consider using the sortrows function afterwards.

5 Comments

Brilliant! Thank you!!
No problem :)
Hi Turlough,
I have another question, how would I then take x and find combinations with m?
This is what i have at the moment:
x = pairCombos(M)
numRows = size(x,1);
C = combnk(1:numRows,2); % Lists every pair combination of row indicies
b = zeros(size(C,1),4); % Preallocate space for variable b
for i = 1:size(C,1)
b(i,:) = [x(C(i,1),:) x(C(i,2),:)]; % generate b
a = [sum(b(i,[1 3])) sum(b(i,[2 4]));a] % I'm suming the values of column 1 & 3 and 2 & 4 of each row
end
l = max(a(:,1))
if l > 500
numRows = size(x,1);
bnumRows = size(b,1);
C1 = combnk(1:numRows,1:bnumRows); % Lists every pair combination of row indicies
b1 = zeros(size(C1,1),6); % Preallocate space for variable b
for i = 1:size(C1,2)
b1(i,:) = [x(C1(i,1),:) b(C1(i,2),:)]; % generate b
a1 = [sum(b1(i,[1 3])) sum(b1(i,[2 4]));a1] % Summing again
[~,ans] = (min(abs(a1 - x))) % trying to find the closest value to l.
end
end
But I get this error:
Error in combnk (line 36)
elseif n < 17 && (k > 3 || n-k < 4).
What do you mean by take x and find combinations with m? Can you explain a bit more about what you're doing?
I'm trying to find the closest value from matrix M to a number L.
So where M = 1 2
3 4
5 6
and all combos so x = 1 2 3 4
1 2 5 6
3 4 5 6
But if I some column 1 and 3 then a= 4 6 8
However if any of these values from a are less than L, then to find combo's of X and M again and so on,
x1 = 1 2 3 4 1 2
1 2 5 6 1 2
3 4 5 6 1 2
1 2 3 4 3 4
1 2 5 6 3 4
3 4 5 6 3 4
I hope I made sense there. But then I realised I can just get combo's for X and X find the sum of column 1 and 3, if that is less than L , then sum 1, 3 and 5, if still less than L then finally sum 1, 3 , 5 and 7.
x2 =1 2 3 4 1 2 3 4
1 2 5 6 1 2 3 4
3 4 5 6 1 2 3 4
......
I was trying to find the optium number of batteries to use in a design with their associated costs, where L was the total voltage required, M column 1 was the volts and column 2 was the hours the batter can run for.
Thank you very much for your help!

Sign in to comment.

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!