How to create a combination of matrix values in pairs?

8 vues (au cours des 30 derniers jours)
Madhav Rawal
Madhav Rawal le 20 Juin 2020
Modifié(e) : Ameer Hamza le 20 Juin 2020
I have a matrix lets say:
A=[1 4;2 5;3 6];
and i want to convert it into a matrix where each value in the first column is matched with each value in the second column:
B= 1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6
is there a vectorized way to do this?

Réponse acceptée

Ameer Hamza
Ameer Hamza le 20 Juin 2020
Modifié(e) : Ameer Hamza le 20 Juin 2020
If you have deep learning toolbox
A=[1 4;2 5;3 6];
B = combvec(A(:,2).', A(:,1).').';
Result
>> B
B =
4 1
5 1
6 1
4 2
5 2
6 2
4 3
5 3
6 3
Alternative without deep learning toolbox
A=[1 4;2 5;3 6];
[i1, i2] = ndgrid(1:size(A,1));
B = [A(i2(:),1) A(i1(:),2)];

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by