matlab: filling matrix diagonalwise [duplicate]

ぃ、小莉子 提交于 2019-12-02 10:17:53
 ix=bsxfun(@plus,[1:n],[n-1:-1:0]'); %generate indices
 A=a(ix);

or

 A=hankel(a) %might be faster than toeplitz because half the matrix is zero
 A(n:-1:1,1:n)

here is what hankel does internally (at least in ML R2013a), adapted to this problem:

c=[1:n];
r=[n-1:-1:0]';
idx=c(ones(n,1),:)+r(:,ones(n,1));
A=a(ix);

I guess the bsxfun solution and what thewaywewalk supposed is the fastest (it's basically the same)

Go with:

n = (numel(a)+1)/2;
A = a(bsxfun(@minus, n+1:n+n, (1:n).'));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!