Matrix with no consecutive nearest neighbours
2 views (last 30 days)
Show older comments
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 Comment
Bruno Luong
on 28 Apr 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
Answers (1)
Steven Lord
on 28 Apr 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.
0 Comments
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!