根据给定矩阵的对角线和反对角线创建新矩阵

时间:2018-12-20 13:30:09

标签: matlab matrix

我要使用以下规则从矩阵B创建矩阵A

  • 非对角元素A类似于B的非对角元素。

  • A的主要对角线是B的反对角线

  • A的反对角线是B的主要对角线。

例如:

A = [ 1  2  3  4;
      7  8  9 10;
     13 14 15 16; 
     19 20 21 22 ];
B = [ 4  2  3  1;
      7  9  8 10;
     13 15 14 16;
     22 20 21 19 ];

如何在给定B的情况下创建A

4 个答案:

答案 0 :(得分:4)

您可以创建所有索引,然后只需一次分配。

{"items":[{"timestamp":"2018-12-20T21:59:41+08:00","cameras":[{"timestamp":"2018-12-20T21:59:21+08:00","image":"https://images.data.gov.sg/api/traffic-images/2018/12/05cd4190-b4e4-430c-b1c1-7047972c09fe.jpg","location":{"latitude":1.27414394350065,"longitude":103.851316802547},"camera_id":"1501","image_metadata":{"height":240,"width":320,"md5":"7bcfabb38b60a18f1380bffca095d6c0"}},{"timestamp":"2018-12-20T21:59:21+08:00","image":"https://images.data.gov.sg/api/traffic-images/2018/12/06c05520-98b6-4086-b691-1bb54ed90482.jpg","location":{"latitude":1.27135090682664,"longitude":103.861828440597},"camera_id":"1502","image_metadata":.....

示例% Get size of square matrix A n = size(A,1); % Indicies are 1:n^2 by default idx = 1:n^2; % Swap diagonal and antidiagonal indices idx( [1:(n+1):n^2, n^2-n+1:1-n:n] ) = [n^2-n+1:1-n:n, 1:(n+1):n^2]; % Use the indexing array to create B from A, reshape to be n*n B = reshape( A( idx ), n, n ); 的输出:

A

答案 1 :(得分:3)

有很多方法可以达到该结果,这只是一个索引练习。对于任何大小为n square 矩阵,这是一种(多种)达到结果的方法:

%% input
A=[ 1  2  3  4 ;
    7  8  9 10 ;
   13 14 15 16 ;
   19 20 21 22 ];

%% Calculate linear indices for the diagonal and antidiagonal
n=size(A,1) ;                   
idxdiag     = 1:(n+1):n^2 ;     % => idxdiag     = [1 6 11 16]
idxantidiag = n:(n-1):n^2-1 ;   % => idxantidiag = [4 7 10 13]

%% Generate B
B = A ;            % start with a simple copy (for the non-diagonal elements)

% Method 1: direct indice assignment
B(idxdiag)     = diag(fliplr(A)) ;  % Assign diagonal elements of B
B(idxantidiag) = flipud(diag(A)) ;  % Assign antidiagonal elements of B

% Method 2: summation
B([idxdiag idxantidiag]) = 0 ;
B = B + diag(diag(fliplr(A))) + fliplr(diag(diag(A))) ;

B =
     4     2     3     1
     7     9     8    10
    13    15    14    16
    22    20    21    19

两个方法都返回完全相同的矩阵B


我建议您熟悉用于了解幕后情况的MATLAB函数:

,可能在以下位置阅读: Matrix Indexing in MATLAB

答案 2 :(得分:3)

我有所不同并得出结论

A=[1 2 3 4;7 8 9 10;13 14 15 16; 19 20 21 22];;
n=size(A,1) ;
B=zeros(n,n) ;

for i=1:n
    for j=1:n
        if i==j
            B(i,j)=A(i,n-i+1);
        elseif j==n-i+1
            B(i,j)=A(i,i);
        else
            B(i,j)=A(i,j);
        end
    end 
end
B

答案 3 :(得分:3)

这是使用eyefindflip来生成linear indices的变体:

ind1 = find(eye(size(A)));
ind2 = flip(find(flip(eye(size(A)))));
B = A;
B([ind1 ind2]) = B([ind2 ind1]);

B =

     4     2     3     1
     7     9     8    10
    13    15    14    16
    22    20    21    19

这是上面的一种变体,仅使用eyeflip来生成logical indices

ind1 = eye(size(A), 'logical');
ind2 = flip(ind1);
B = A;
B(ind1) = flip(A(ind2));
B(ind2) = flip(A(ind1));

B =

     4     2     3     1
     7     9     8    10
    13    15    14    16
    22    20    21    19