Renumber matrix with some restrictions

4 vues (au cours des 30 derniers jours)
Antonis Ventouris
Antonis Ventouris le 5 Jan 2022
Hello everyone,
i have a problem with a matrix called conn which represents the names of some cordinates.
The conn matrix is: conn=[1 5; 5 6; 6 7; 7 2; 2 3; 2 8; 8 9 ; 9 10; 10 4]
i want to renumber it from the start so the new matrix Con would be:
Con = [1 2; 2 3; 3 4; 4 5; 5 6; 5 7; 7 8 ; 8 9; 9 10].
So i want to renumber it from start row by row but all the same numbers from conn matrix apprear in the Con with the same number (see 2 3 and 2 8 from conn). Is it possible?
Thanks in advance.

Réponse acceptée

Walter Roberson
Walter Roberson le 5 Jan 2022
Modifié(e) : Walter Roberson le 5 Jan 2022
conn=[1 5; 5 6; 6 7; 7 2; 2 3; 2 8; 8 9 ; 9 10; 10 4]
conn = 9×2
1 5 5 6 6 7 7 2 2 3 2 8 8 9 9 10 10 4
[~, ~, ic] = unique(conn.', 'stable')
ic = 18×1
1 2 2 3 3 4 4 5 5 6
Con = reshape(ic, 2, []).'
Con = 9×2
1 2 2 3 3 4 4 5 5 6 5 7 7 8 8 9 9 10
  1 commentaire
Antonis Ventouris
Antonis Ventouris le 5 Jan 2022
you are a star! thank you!

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 5 Jan 2022
I'm not completely sure how you got from conn to Con, in particular I'm not sure how you generated the third row in Con. [3 4] doesn't appear as a row in conn. What I think from your description you want uses sort and sortrows.
conn=[1 5; 5 6; 6 7; 7 2; 2 3; 2 8; 8 9 ; 9 10; 10 4]
conn = 9×2
1 5 5 6 6 7 7 2 2 3 2 8 8 9 9 10 10 4
smallerNumbersFirstInRow = sort(conn, 2)
smallerNumbersFirstInRow = 9×2
1 5 5 6 6 7 2 7 2 3 2 8 8 9 9 10 4 10
Con = sortrows(smallerNumbersFirstInRow)
Con = 9×2
1 5 2 3 2 7 2 8 4 10 5 6 6 7 8 9 9 10
If that's not what you want can you describe in more detail exactly how you generated your Con matrix from the conn matrix?

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