Multiple combinations of a matrix
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ulika Naidoo
le 24 Avr 2020
Commenté : Ulika Naidoo
le 25 Avr 2020
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 commentaires
Réponse acceptée
Turlough Hughes
le 24 Avr 2020
Modifié(e) : Turlough Hughes
le 25 Avr 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 commentaires
Turlough Hughes
le 25 Avr 2020
What do you mean by take x and find combinations with m? Can you explain a bit more about what you're doing?
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Audio Processing Algorithm Design dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!