在Matlab中获得矩阵的对角线

Ale*_* R. 4 performance matlab matrix diagonal

A大小矩阵[n,n].如果我想提取它的对角线,我会这样做diag(A).

实际上,我想要相反的对角线,这将是[A(n,1),A(n-1,2),A(n-2,3),...].

一种方法是通过diag(flipud(A)).然而,flipud(A)与找到通常的对角线相比,它相当浪费并且花费的时间增加了10倍.

我正在寻找一种快速获得对角线的方法.当然,for循环看起来非常缓慢.建议将不胜感激.

the*_*eye 10

这是我的矩阵,由A = magic制作(5)

A =

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


s = size(A,1)
A(s:s-1:end-1)

ans =
11    12    13    14    15
Run Code Online (Sandbox Code Playgroud)


Amr*_*mro 6

下面是对目前为止提到的所有方法的比较,以及我能想到的其他一些变化.这是使用TIMEIT功能在64位R2013a上测试的.

function [t,v] = testAntiDiag()
    % data and functions
    A = magic(5000);
    f = {
        @() func0(A) ;
        @() func1(A) ;
        @() func2(A) ;
        @() func3(A) ;
        @() func4(A) ;
        @() func5(A) ;
        @() func6(A) ;
        @() func7(A) ;
    };

    % timeit and check results
    t = cellfun(@timeit, f, 'UniformOutput',true);
    v = cellfun(@feval, f, 'UniformOutput',false);
    assert( isequal(v{:}) )
end


function d = func0(A)
    d = diag(A(end:-1:1,:));
end

function d = func1(A)
    d = diag(flipud(A));
end

function d = func2(A)
    d = flipud(diag(fliplr(A)));
end

function d = func3(A)
    d = diag(rot90(A,3));
end

function d = func4(A)
    n = size(A,1);
    d = A(n:n-1:end-1).';
end

function d = func5(A)
    n = size(A,1);
    d = A(cumsum(n + [0,repmat(-1,1,n-1)])).';
end

function d = func6(A)
    n = size(A,1);
    d = A(sub2ind([n n], n:-1:1, 1:n)).';
end

function d = func7(A)
    n = size(A,1);
    d = zeros(n,1);
    for i=1:n
        d(i) = A(n-i+1,i);
    end
end
Run Code Online (Sandbox Code Playgroud)

时间安排(按照上面定义的顺序):

>> testAntiDiag
ans =
   0.078635867152801
   0.077895631970976    % @AlexR.
   0.080368641824528
   0.195832501156751
   0.000074983294297    % @thefourtheye
   0.000143019460665    % @woodchips
   0.000174679680437
   0.000152488508547    % for-loop
Run Code Online (Sandbox Code Playgroud)

对我来说最令人惊讶的结果是最后一个.显然JIT编译对这种简单的for循环非常有效.