有没有一种简便的方法来生成以下矩阵:
a =
4 5 6 7
3 4 5 6
2 3 4 5
1 2 3 4
如何投影向量[1 2 3 4 5 6 7]到对角线上?
谢谢。
a = flipud(hankel(1:4,4:7));
a = toeplitz(fliplr(1:4),4:7);
a = toeplitz(4:-1:1,4:7); %# Without fliplr
>> vec = [6 3 45 1 1 2]; %# A sample vector
>> centerIndex = 3;
>> a = flipud(hankel(vec(1:centerIndex),vec(centerIndex:end)))
a =
45 1 1 2
3 45 1 1
6 3 45 1
考虑使用这个替代方案:
a = bsxfun(@plus, (4:-1:1)', 0:3)
x = randi(50, [1 10])
num = 5;
idx = bsxfun(@plus, (num:-1:1)', 0:(numel(x)-num));
a = x(idx)
带有示例输出的代码:
x =
41 46 7 46 32 5 14 28 48 49
a =
32 5 14 28 48 49
46 32 5 14 28 48
7 46 32 5 14 28
46 7 46 32 5 14
41 46 7 46 32 5