Multiple combinations of a matrix

1 次查看(过去 30 天)
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 个评论
Turlough Hughes
Turlough Hughes 2020-4-24
Do you want to be able to do this generally for any number of rows?
Ulika Naidoo
Ulika Naidoo 2020-4-24
Yes, for any number of rows please.

请先登录,再进行评论。

采纳的回答

Turlough Hughes
Turlough Hughes 2020-4-24
编辑:Turlough Hughes 2020-4-25
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 个评论
Turlough Hughes
Turlough Hughes 2020-4-25
What do you mean by take x and find combinations with m? Can you explain a bit more about what you're doing?
Ulika Naidoo
Ulika Naidoo 2020-4-25
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Audio Processing Algorithm Design 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by