Matrix with no consecutive nearest neighbours

2 次查看(过去 30 天)
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 个评论
Bruno Luong
Bruno Luong 2021-4-28
"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

请先登录,再进行评论。

回答(1 个)

Steven Lord
Steven Lord 2021-4-28
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.

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by