我需要在MATLAB中构建一个名为H的toeplitz矩阵,其中H的大小为256 x 256,来自大小为64 x 1的向量h。我需要用H和l-th element列的H-th element行填充矩阵H,如下所示:
H(l,p) = h(l-p) if 0 <= (l-p) =< 64。否则,返回H(l,p)=0;
我编写的代码如下:
h = randn(64,1);
H = zeros(256,256);
for l= 1:256
for p = 1 : 256
if (l-p <= 64 && l-p >= 0)
H(l,p) = h(l-p);
end
end
end但是,我认为代码中有一个错误,因为它没有给我预期的结果。
如何获得toeplitz矩阵?
发布于 2020-08-14 17:16:36
l-p=0出现了一个问题,因为h(0)会调用h的第0个元素,而且由于MATLAB使用从1开始的索引,所以会崩溃。只需删除检查中的等号:
h = randn(64,1);
H = zeros(256,256);
for l= 1:256
for p = 1 : 256
if (l-p <= 64 && l-p > 0) % Removed the = sign
H(l,p) = h(l-p);
end
end
end发布于 2020-08-14 17:14:22
发布于 2020-08-14 22:29:38
下面是一种手动使用vectorization和implicit expansion而不是循环的方法:
t = 1:numel(h);
H = h(abs(t-t.')+1); https://stackoverflow.com/questions/63409806
复制相似问题