Effacer les filtres
Effacer les filtres

Matrix with no consecutive nearest neighbours

2 vues (au cours des 30 derniers jours)
Duarte Guerreiro
Duarte Guerreiro le 28 Avr 2021
Commenté : Bruno Luong le 28 Avr 2021
Hello, everyone,
Is there any way to create a matrix (n x n) where the numbers ( from 0 to n) are placed randomly but with the restriction that a number x does not have x+1 or x-1 as a nearest neighbours (considering 8 neighbours...)
Example of failure:
1 2 4
5 3 7
9 10 12
Thank you.
Duarte
  1 commentaire
Bruno Luong
Bruno Luong le 28 Avr 2021
"Is there any way to create a matrix (n x n) where the numbers ( from 0 to n) "
In your example n is 3 but values are up to 12.
Please explain again correctly

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 28 Avr 2021
For a 3-by-3 assuming you mean the numbers from 0 to 8 or 1 to 9 the answer is no. The center element is 8-connected to all the other elements of the matrix. Even if you put 0, 1, 8, or 9 in the center the elements 1, 2, 7, or 8 must be adjacent to them respectively.
I have not thought much about larger values of n. Try generating a random n-by-n matrix with randperm and reshape and testing if it satisfies the requirement. The diff function will help you at least with the 4-connected elements. If you try a large number of trials and reject them all as violating your criteria, either it's not possible or the number of valid arrangements is very small and you haven't hit one yet.

Community Treasure Hunt

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

Start Hunting!

Translated by