sn *_* at 3 matlab matrix symmetry diagonal
我有一个7*1的矢量a = (1:7).'.我想A从矢量形成一个大小为4*4 的矩阵,使得形成矩阵的反对角线a的元素如下:aA
A = [1 2 3 4;
2 3 4 5;
3 4 5 6;
4 5 6 7]
Run Code Online (Sandbox Code Playgroud)
我希望这适用于一般a,而不仅仅是元素是连续整数.
我感谢任何帮助.
添加两个输出meshgrid可以给出索引:
[x, y] = meshgrid(1:4, 0:3);
x + y;
% ans = [1 2 3 4
% 2 3 4 5
% 3 4 5 6
% 4 5 6 7];
Run Code Online (Sandbox Code Playgroud)
如果a在你的例子中,你可以在那里停止.或者,使用它来索引一般向量a.为了比较,我将使用与rahnema1相同的示例输入为他们的方法:
a = [4 6 2 7 3 5 1];
[x, y] = meshgrid(1:4, 0:3);
A = a(x + y);
% A = [4 6 2 7
% 6 2 7 3
% 2 7 3 5
% 7 3 5 1]
Run Code Online (Sandbox Code Playgroud)
有很多方法可以创建,而不是使用索引meshgrid,请参见下面的标杆功能对于一些exampels!
下面是一些定时运行不同的方法,包括使用方法cumsum,repmat,hankel一个简单的和for循环.这个基准测试是在Matlab 2015b中完成的,因此利用了Matlab优化等优点,而rahnema1的答案中的Octave基准可能无法做到.我也使用timeit比tic/ 更健壮的功能,toc因为它进行多次试验等.
function benchie()
n = 10000; % (large) square matrix size
a = 1:2*n-1; % array of correct size, could be anything this long
f1 = @() m1(a,n); disp(['bsxfun: ', num2str(timeit(f1))]);
f2 = @() m2(a,n); disp(['cumsum: ', num2str(timeit(f2))]);
f3 = @() m3(a,n); disp(['meshgrid: ', num2str(timeit(f3))]);
f4 = @() m4(a,n); disp(['repmat: ', num2str(timeit(f4))]);
f5 = @() m5(a,n); disp(['for loop: ', num2str(timeit(f5))]);
f6 = @() m6(a,n); disp(['hankel1: ', num2str(timeit(f6))]);
f7 = @() m7(a,n); disp(['hankel2: ', num2str(timeit(f7))]);
end
% Use bsxfun to do broadcasting of addition
function m1(a,n); A = a(bsxfun(@plus, (1:n), (0:n-1).')); end
% Use cumsum to do cumulative vertical addition to create indices
function m2(a,n); A = a(cumsum([(1:n); ones(n-1,n)])); end
% Add the two meshgrid outputs to get indices
function m3(a,n); [x, y] = meshgrid(1:n, 0:n-1); A = a(x + y); end
% Use repmat twice to replicate the meshgrid results, for equivalent one liner
function m4(a,n); A = a(repmat((1:n)',1,n) + repmat(0:n-1,n,1)); end
% Use a simple for loop. Initialise A and assign values to each row in turn
function m5(a,n); A = zeros(n); for ii = 1:n; A(:,ii) = a(ii:ii+n-1); end; end
% Create a Hankel matrix (constant along anti-diagonals) for indexing
function m6(a,n); A = a(hankel(1:n,n:2*n-1)); end
% Create a Hankel matrix directly from elements
function m7(a,n); A = hankel(a(1:n),a(n:2*n-1)); end
Run Code Online (Sandbox Code Playgroud)
输出:
bsxfun: 1.4397 sec
cumsum: 2.0563 sec
meshgrid: 2.0169 sec
repmat: 1.8598 sec
for loop: 0.4953 sec % MUCH quicker!
hankel1: 2.6154 sec
hankel2: 1.4235 sec
Run Code Online (Sandbox Code Playgroud)
所以你最好使用rahnema1的建议bsxfun或直接生成一个hankel矩阵,如果你想要一个衬里,这里有一个很棒的StackOverflow答案,它解释了一些bsxfun优点:在Matlab中,何时使用bsxfun是最佳的?
但是,for循环速度快了两倍!结论:Matlab有很多简洁的方法来实现这样的事情,有时候是一个简单的for循环,有一些适当的预分配,Matlab的内部优化可能是最快的.
你可以使用hankel:
n= 4;
A= hankel(a(1:n),a(n:2*n-1))
Run Code Online (Sandbox Code Playgroud)
其他解决方案(扩展/ bsxfun):
在MATLAB中r2016b/Octave它可以创建为:
A = a((1:4)+(0:3).')
Run Code Online (Sandbox Code Playgroud)
在r2016b之前,您可以使用bsxfun:
A = a(bsxfun(@plus,1:4, (0:3).'))
Run Code Online (Sandbox Code Playgroud)
输入/输出示例
a = [4 6 2 7 3 5 1]
A =
4 6 2 7
6 2 7 3
2 7 3 5
7 3 5 1
Run Code Online (Sandbox Code Playgroud)
使用@Wolfie在Octave中测试的基准测试:
_____________________________________
|Method |memory peak(MB)|timing(Sec)|
|=========|===============|===========|
|bsxfun |2030 |1.50 |
|meshgrid |3556 |2.43 |
|repmat |2411 |2.64 |
|hankel |886 |0.43 |
|for loop |886 |0.82 |
Run Code Online (Sandbox Code Playgroud)