在Matlab中,有没有办法将矩阵的下三角半部分复制到上三角半?

时间:2015-07-27 03:55:14

标签: matlab matrix sparse-matrix

在Matlab中,有没有办法将矩阵的下三角半部分复制到上三角半?

对于方阵A,我希望能够做到

triu(A)=tril(A)';

为i>设置所有A(i,j)A(j,i)学家

有一种方便/有效的方法吗?

注意:最好,答案可以应用于稀疏矩阵。

关于效率的话题,

我做了一些关于访问部分矩阵的相对时间成本的测试。我使用的是版本R2014a

一些结果:逻辑索引非常缓慢,应该避免;稀疏矩阵的逻辑索引更糟糕;在稀疏矩阵环境下加入密集矩阵的加法是错误的,应该不惜一切代价避免。

到目前为止,数据表明tril(A,-1)+tril(A)'可能是最好的方法。 (循环尚未尝试。但它可能至少与逻辑索引一样慢。)

% Note: In each case, the posted results are those after a few "warm up" trials.

% providing the arrays
>> e=ones(1000,1);
>> temp=[e,zeros(1000,999)];
>> A=magic(1000);
>> temp2=[true(1000,1),false(1000,999)];

% matrix addition (without sparse matrix support)
>> tic;for i=1:1000; A=A+temp; A=A-temp; end; toc
Elapsed time is 1.903718 seconds.
>> tic;for i=1:1000; A=A+temp; A=A-temp; end; toc
Elapsed time is 1.898125 seconds.
>> tic;for i=1:1000; A=A+temp; A=A-temp; end; toc
Elapsed time is 1.896766 seconds.

% logical indexing to modify part of a matrix by a smaller matrix (a column vector)
>> tic;for i=1:1000; A(temp2)=A(temp2)+e; A(temp2)=A(temp2)-e; end; toc
Elapsed time is 4.916888 seconds.
>> tic;for i=1:1000; A(temp2)=A(temp2)+e; A(temp2)=A(temp2)-e; end; toc
Elapsed time is 4.926484 seconds.
>> tic;for i=1:1000; A(temp2)=A(temp2)+e; A(temp2)=A(temp2)-e; end; toc
Elapsed time is 4.929350 seconds.

% logical indexing to modify part of a matrix by a scalar
>> tic;for i=1:1000; A(temp2)=A(temp2)+1; A(temp2)=A(temp2)-1; end; toc
Elapsed time is 4.914185 seconds.
>> tic;for i=1:1000; A(temp2)=A(temp2)+1; A(temp2)=A(temp2)-1; end; toc
Elapsed time is 4.909323 seconds.
>> tic;for i=1:1000; A(temp2)=A(temp2)+1; A(temp2)=A(temp2)-1; end; toc
Elapsed time is 4.905367 seconds.
>> tic;for i=1:1000; A(temp2)=1; A(temp2)=-1; end; toc
Elapsed time is 2.472018 seconds.
>> tic;for i=1:1000; A(temp2)=1; A(temp2)=-1; end; toc
Elapsed time is 2.463884 seconds.
>> tic;for i=1:1000; A(temp2)=1; A(temp2)=-1; end; toc
Elapsed time is 2.462588 seconds.

% matrix addition with sparse matrix support (astounding?)
>> A=sparse(A); temp3=sparse(temp2);
>> tic;for i=1:1000; A=A+temp3; A=A-temp3; end; toc
Elapsed time is 13.648472 seconds.
>> tic;for i=1:1000; A=A+temp3; A=A-temp3; end; toc
Elapsed time is 13.485242 seconds.
>> tic;for i=1:1000; A=A+temp3; A=A-temp3; end; toc
Elapsed time is 13.551307 seconds.

% matrix addition with sparse matrix support between matrices with identical sparsity structure
>> tic;for i=1:1000; temp3=temp3+temp3; temp3=temp3-temp3; end; toc
Elapsed time is 0.013174 seconds.
>> tic;for i=1:1000; temp3=temp3+temp3; temp3=temp3-temp3; end; toc
Elapsed time is 0.018456 seconds.
>> tic;for i=1:1000; temp3=temp3+temp3; temp3=temp3-temp3; end; toc
Elapsed time is 0.009555 seconds.

% matrix addition with sparsity support between two very sparse matrix of completely different sparsity structure
>> temp4=sparse([zeros(1000,999),ones(1000,1)]);
>> tic;for i=1:1000; temp4=temp4+temp3; temp4=temp4-temp3; end; toc
Elapsed time is 0.019596 seconds.
>> tic;for i=1:1000; temp4=temp4+temp3; temp4=temp4-temp3; end; toc
Elapsed time is 0.014397 seconds.
>> tic;for i=1:1000; temp4=temp4+temp3; temp4=temp4-temp3; end; toc
Elapsed time is 0.010127 seconds.
>> tic;for i=1:1000; temp4=temp4+temp3; temp4=temp4-temp3; end; toc

% logical indexing with very sparse matrix
>> tic;for i=1:1000; temp4(temp2)=1; temp4(temp2)=-1; end; toc
Elapsed time is 6.333907 seconds.
>> tic;for i=1:1000; temp4(temp2)=1; temp4(temp2)=-1; end; toc
Elapsed time is 6.378107 seconds.
>> tic;for i=1:1000; temp4(temp2)=1; temp4(temp2)=-1; end; toc
Elapsed time is 6.486917 seconds.

% cost for creating logical arrays
>> tic;temp2=[true(10000,1),false(10000,9999)];toc
Elapsed time is 0.060349 seconds.
>> tic;temp2=[true(10000,1),false(10000,9999)];toc
Elapsed time is 0.063874 seconds.
>> tic;temp2=[true(10000,1),false(10000,9999)];toc
Elapsed time is 0.060837 seconds.

2 个答案:

答案 0 :(得分:4)

您可以尝试tril(A,-1)+tril(A)'

>> A = rand(3);

A =

    0.2277    0.9234    0.9049
    0.4357    0.4302    0.9797
    0.3111    0.1848    0.4389

>> tril(A,-1)+tril(A)'

ans =

    0.2277    0.4357    0.3111
    0.4357    0.4302    0.1848
    0.3111    0.1848    0.4389

此外:

A(triu(true(3),1)) = A(tril(true(3),-1))

可能还有另一种变化......

答案 1 :(得分:3)

这可能是bsxfun使用logical indexing设置上三角形元素与下层元素相同的一种方法 -

%// Get size of square-sized input array
N = size(A,1);  

%// Create lower triangular mask
mask = bsxfun(@gt,[1:N]',[1:N]) %//'

%// Replace transposed masked (upper triangular) elements with lower ones 
A(mask.') = A(mask)

就变量创建而言,还有一个额外的逻辑数组mask正在创建,并且通过函数调用,还会添加额外的bsxfun

示例运行 -

A =
      0.39223      0.70605     0.046171
      0.65548     0.031833     0.097132
      0.17119      0.27692      0.82346
mask =
     0     0     0
     1     0     0
     1     1     0
A =
      0.39223      0.65548      0.17119
      0.65548     0.031833      0.27692
      0.17119      0.27692      0.82346

对于与比较基于tril/triubsxfun的等效掩码创建相关的效果数字,也可以引用Benchmarks comparing BSXFUN and TRIU