matlab中的矩阵的顺序 [英] order making for a matrix in matlab

查看:618
本文介绍了matlab中的矩阵的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我在matlab中使用了bwdist ,然后对获得的矩阵排序,但结果不是我想要的结果。

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

更好的解决方案吗?

推荐答案

当元素在中心时,只需使用 spiral 命令:

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

让我们利用这个奇特的 spiral 函数。要获得答案矩阵 A ,请制作更大的矩阵 M ,其中起点在中心。注意, A M 中的元素的相对顺序是相同的。我们需要的是从 M 中获取 A 作为子矩阵,并以相同的顺序填充连续的元素数组:

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:

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

以下是它的工作原理:

>> 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

这不是最快的解决方案,因为它需要排序,因此需要 O(N ^ 2 logN) code> O(N ^ 2)实现。但它是非常短,工作足够快的矩阵约1000x1000。

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.

这篇关于matlab中的矩阵的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆