【问题标题】:transforming a matrix into a vector along its diagonals将矩阵沿其对角线转换为向量
【发布时间】:2011-03-27 07:41:17
【问题描述】:

我不是程序员,我只需要在 matlab 中解决一些数字问题。 我需要一个函数来对任何方阵进行以下转换:

来自

row 1: 1 2 3 
row 2: 4 5 6
row 3: 7 8 9

1 4 2 7 5 3 8 6 9

即,将矩阵沿其对角线从左到右写在一个向量中。 请问有什么想法吗?


不过,我真的需要更多帮助:

假设我们已经转换为向量的矩阵具有由 M(i,j) 表示的条目,其中 i 是行和 j 列。现在我需要能够从向量中的一个位置找出矩阵中的原始位置,即如果它在向量中的第三个条目,我需要一个函数,它会给我 i=1 j=2。请问有什么想法吗?我真的坚持这个:(谢谢

【问题讨论】:

    标签: algorithm matlab matrix transform


    【解决方案1】:

    这是一种方法。

    %# n is the number of rows (or cols) of the square array
    n = 3;
    array = [1 2 3;4 5 6;7 8 9]; %# this is the array we'll reorder
    
    %# create list of indices that allow us
    %# to read the array in the proper order
    hh = hankel(1:n,n:(2*n-1)); %# creates a matrix with numbered antidiagonals
    [dummy,sortIdx] = sort(hh(:)); %# sortIdx contains the new order
    
    %# reorder the array
    array(sortIdx)
    
    ans =
         1     4     2     7     5     3     8     6     9
    

    【讨论】:

    • 不需要调用 BSXFUN。您可以通过 sort(hh(:)) 获取索引向量。
    【解决方案2】:

    这与previous question 以之字形顺序遍历矩阵非常相似。稍作修改,我们得到:

    A = rand(3);                        %# input matrix
    
    ind = reshape(1:numel(A), size(A)); %# indices of elements
    ind = spdiags(fliplr(ind));         %# get the anti-diagonals
    ind = ind(end:-1:1);                %# reverse order
    ind = ind(ind~=0);                  %# keep non-zero indices
    B = A(ind);                         %# get elements in desired order
    

    使用SPDIAGS 函数。这样做的好处是它适用于任意矩阵大小(不仅仅是方阵)。示例:

    A =
          0.75127      0.69908      0.54722      0.25751
           0.2551       0.8909      0.13862      0.84072
          0.50596      0.95929      0.14929      0.25428
    B =
      Columns 1 through 6
          0.75127       0.2551      0.69908      0.50596       0.8909      0.54722
      Columns 7 through 12
          0.95929      0.13862      0.25751      0.14929      0.84072      0.25428
    

    【讨论】:

      【解决方案3】:

      您可以使用函数HANKEL 将矩阵转换为向量,以生成矩阵的索引。这是Jonas' answer 的简化版本,使用M 作为上面给出的示例矩阵:

      N = size(M,1);
      A = hankel(1:N,N:(2*N-1));
      [junk,sortIndex] = sort(A(:));
      

      现在,您可以使用 sortIndex 将矩阵 M 更改为向量 vec,如下所示:

      vec = M(sortIndex);
      

      如果您想将行和列索引(rIndexcIndex)获取到与vec 中的值相对应的原始矩阵中,可以使用函数IND2SUB

      [rIndex,cIndex] = ind2sub(N,sortIndex);
      

      【讨论】:

        【解决方案4】:

        你可以这样生成对角线:

        for i = -2:2
            diag(flipud(a), i)
        end
        

        我不知道这是否是连接对角线的最佳方式:

        d = []
        for i = -2:2
            d = vertcat(d, diag(flipud(a), i))
        end
        

        (我在八度音阶中测试过,而不是在matlab中)

        【讨论】:

          【解决方案5】:
          A=[1,2,3;4,5,6;7,8,9];
          d = size(A,1);
          X=[];
          for n = 1:2*size(A,1) - 1
              j = min(n,d); i = (n+1)-(j);
              X = cat(2,X,diag(flipud(A(i:j,i:j)))');
          end
          
          X
          X =
               1     4     2     7     5     3     8     6     9
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-01-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多