在matlab中订购矩阵

时间:2023-01-18 12:51:36

I am doing a simulation on a matrix (suppose a 5x5 matrix). One of the elements of this matrix is known (the back square in below; this location will not be always in center) and I want to start from that location and spirally visit the other elements (I have shown the orders in number). How I can define this order in a large matrix (e.g. 1000x1000)? Because I cannot do it manually and I am looking for a more heuristic way.

我正在对矩阵进行模拟(假设是5x5矩阵)。这个矩阵的一个元素是已知的(下面的后方;这个位置不会总是在中心)我想从那个位置开始并螺旋地访问其他元素(我已经显示了数量上的订单)。如何在大矩阵(例如1000x1000)中定义此顺序?因为我不能手动完成它,我正在寻找一种更具启发性的方式。

I used bwdist in matlab and then sort the obtained matrix, but the results were not as what I want.

我在matlab中使用bwdist然后对获得的矩阵进行排序,但结果并不像我想要的那样。

Any better solution?

更好的解决方案?

在matlab中订购矩阵

1 个解决方案

#1


3  

When element is in the center, just use the spiral command:

当元素位于中心时,只需使用螺旋命令:

>> spiral(5)

ans =

    21    22    23    24    25
    20     7     8     9    10
    19     6     1     2    11
    18     5     4     3    12
    17    16    15    14    13

For arbitrary position of the starting point we'll need to do something by hands

对于起点的任意位置,我们需要手工做

Let's exploit this fancy spiral function. To obtain the answer matrix A, make the bigger matrix M where the starting point is in the center. Note that relative order of the elements in A and in M is the same. All we need is to get A as a submatrix from M and fill it with continuous array of elements in the same order:

让我们利用这个奇特的螺旋函数。要获得答案矩阵A,请使用较大的矩阵M,其中起点位于中心。注意,A和M中元素的相对顺序是相同的。我们所需要的只是从A中获取A作为子矩阵,并以相同的顺序用连续的元素数组填充它:

function A = spiral_generic(n, P)
% Makes NxN matrix filled up spirally starting with point P
  r = max([P - 1, n - P]);              % Radius of the bigger matrix
  M = spiral(2 * r + 1);                % Bigger matrix itself
  C = r + 1 - (P - 1);                  % Top-left corner of A in M
  A = M(C(1):C(1)+n-1, C(2):C(2)+n-1);  % Get the submatrix
  [~, order] = sort(A(:));              % Get elements' order
  A(order) = 1:n^2;                     % Fill with continous values
end

And here's how it works:

以下是它的工作原理:

>> spiral_generic(5, [3 2])

ans =

    17    18    19    20    21
     7     8     9    10    22
     6     1     2    11    23
     5     4     3    12    24
    16    15    14    13    25

>> spiral_generic(6, [2 5])

ans =

    36    25    16     7     8     9
    35    24    15     6     1     2
    34    23    14     5     4     3
    33    22    13    12    11    10
    32    21    20    19    18    17
    31    30    29    28    27    26

This is not the fastest solution since it requires sorting and thus takes O(N^2 logN) time comparing to direct O(N^2) implementation. But it is very short and works fast enough for matrices around 1000x1000.

这不是最快的解决方案,因为它需要排序,因此与直接O(N ^ 2)实现相比需要O(N ^ 2 logN)时间。但它非常短,并且对于1000x1000左右的矩阵来说足够快。

#1


3  

When element is in the center, just use the spiral command:

当元素位于中心时,只需使用螺旋命令:

>> spiral(5)

ans =

    21    22    23    24    25
    20     7     8     9    10
    19     6     1     2    11
    18     5     4     3    12
    17    16    15    14    13

For arbitrary position of the starting point we'll need to do something by hands

对于起点的任意位置,我们需要手工做

Let's exploit this fancy spiral function. To obtain the answer matrix A, make the bigger matrix M where the starting point is in the center. Note that relative order of the elements in A and in M is the same. All we need is to get A as a submatrix from M and fill it with continuous array of elements in the same order:

让我们利用这个奇特的螺旋函数。要获得答案矩阵A,请使用较大的矩阵M,其中起点位于中心。注意,A和M中元素的相对顺序是相同的。我们所需要的只是从A中获取A作为子矩阵,并以相同的顺序用连续的元素数组填充它:

function A = spiral_generic(n, P)
% Makes NxN matrix filled up spirally starting with point P
  r = max([P - 1, n - P]);              % Radius of the bigger matrix
  M = spiral(2 * r + 1);                % Bigger matrix itself
  C = r + 1 - (P - 1);                  % Top-left corner of A in M
  A = M(C(1):C(1)+n-1, C(2):C(2)+n-1);  % Get the submatrix
  [~, order] = sort(A(:));              % Get elements' order
  A(order) = 1:n^2;                     % Fill with continous values
end

And here's how it works:

以下是它的工作原理:

>> spiral_generic(5, [3 2])

ans =

    17    18    19    20    21
     7     8     9    10    22
     6     1     2    11    23
     5     4     3    12    24
    16    15    14    13    25

>> spiral_generic(6, [2 5])

ans =

    36    25    16     7     8     9
    35    24    15     6     1     2
    34    23    14     5     4     3
    33    22    13    12    11    10
    32    21    20    19    18    17
    31    30    29    28    27    26

This is not the fastest solution since it requires sorting and thus takes O(N^2 logN) time comparing to direct O(N^2) implementation. But it is very short and works fast enough for matrices around 1000x1000.

这不是最快的解决方案,因为它需要排序,因此与直接O(N ^ 2)实现相比需要O(N ^ 2 logN)时间。但它非常短,并且对于1000x1000左右的矩阵来说足够快。