如何在Matlab中使用信号样本的移位版本创建矩阵? [英] how to create the matrix with the shifted version of the signal samples in Matlab?

查看:92
本文介绍了如何在Matlab中使用信号样本的移位版本创建矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大小为25001x1的矢量,它代表我的一个信号样本,并且我想创建矩阵,使得矩阵的第一列是我的矢量或信号,而其余各列是原始信号的移位版本或向量.我可以通过以下方式(将10x1的向量用于理解目的)通过加零来完成.

I have vector of size 25001x1 which represent one of my signal samples and i want to create the matrix such way that first column of the matrix is my vector or signal and the rest of each column is shifted version of the the original signal or vector.which i accomplish with following way(the vector of 10x1 is consider for the understanding purpose) by adding zeros.

 y=[1 2 3 4 5 6 7 8  9 10 ]';

 A=zeros(10,10);

 for i=1:length(y)
 A(:,i)=y;
 y=[1 2 3 4 5 6 7 8  9 10 ]';
 y=[zeros(1,(i))';y(1:end-(i))];

 end  

这将导致

>> A

 A =

 1     0     0     0     0     0     0     0     0     0
 2     1     0     0     0     0     0     0     0     0
 3     2     1     0     0     0     0     0     0     0
 4     3     2     1     0     0     0     0     0     0
 5     4     3     2     1     0     0     0     0     0
 6     5     4     3     2     1     0     0     0     0
 7     6     5     4     3     2     1     0     0     0
 8     7     6     5     4     3     2     1     0     0
 9     8     7     6     5     4     3     2     1     0
10     9     8     7     6     5     4     3     2     1

现在,当我想移动多个元素或想要添加多个零时,我做了以下

Now when i want to shift more than one element or wanted to add more than one zero i did the following

y=[1 2 3 4 5 6 7 8  9 10 11 ]';

A=zeros(11,11);

for i=1:length(y)
A(:,i)=y;
y=[1 2 3 4 5 6 7 8  9 10 11  ]';
y=[zeros(1,(i*2))';y(1:end-(i*2))];
if (i>=(length(y)/2))
    break;
end 


end 

这将导致以下

>> A

A =

 1     0     0     0     0     0     0     0     0     0     0
 2     0     0     0     0     0     0     0     0     0     0
 3     1     0     0     0     0     0     0     0     0     0
 4     2     0     0     0     0     0     0     0     0     0
 5     3     1     0     0     0     0     0     0     0     0
 6     4     2     0     0     0     0     0     0     0     0
 7     5     3     1     0     0     0     0     0     0     0
 8     6     4     2     0     0     0     0     0     0     0
 9     7     5     3     1     0     0     0     0     0     0
10     8     6     4     2     0     0     0     0     0     0
11     9     7     5     3     1     0     0     0     0     0

是否有任何泛化方法可以做同样的事情而无需使用for循环?

Is there any generalize method to do same thing and that too without using for loop?

推荐答案

问题的第一部分已得到回答

The first part of your question is answered in this other Q&A. The fastest method there (not the accepted answer) is as follows:

N = numel(A);
val = repmat([A(:);0],1,N).*bsxfun(@le,[1:N+1]',[N:-1:1]);
out = reshape(val(1:N*N),N,N);

对于MATLAB R2016b及更高版本,我们可以对其进行现代化:

For MATLAB R2016b and newer we can modernize that:

N = numel(A);
val = repmat([A(:);0],1,N) .* ((1:N+1).' <= (N:-1:1));
out = reshape(val(1:N*N),N,N);

(我只是将bsxfun(@le,x,y)替换为x<=y,因为几年前在这些情况下不再需要使用bsxfun.我还删除了多余的串联运算符[],并替换了'使用.',对于这种用法更正确.)

(I simply replaced bsxfun(@le,x,y) with x<=y, since a few years ago there is no longer a need to use bsxfun in these cases. I also removed redundant concatenation operators [], and replaced ' with .', which is more correct for this use.)

对于您的问题的第二部分,我们需要以不平凡的方式来概括上面的代码.以下代码是其结果:

For the second part of your question, we need to generalize the code above in a non-trivial manner. The following code is the result of that:

N = numel(A);
step = 2;          % Set this to however many zeros you want to add each column
indx = N:-step:1;
M = numel(indx);
val = (1:N+step).' <= indx; % use bsxfun(@le, (1:N+step).',indx) instead for older MATLAB
val = repmat([A(:);zeros(step,1)],1,M).* val;
out = reshape(val(1:N*M),N,[]);

我已将N:-1:1替换为N:-step:1,这是主要更改.我还需要将step零添加到A,而不是仅添加一个(这是[A(:);zeros(step,1)],之前是[A(:);0]).而且我调整了各处的大小,以解决较小的输出数组.

I've replaced N:-1:1 with N:-step:1, this is the main change. I also needed to add step zeros to A, instead of only one (this is the [A(:);zeros(step,1)], where before it was [A(:);0]). And I adjusted sizes everywhere to account for the smaller output array.

请注意,这不会产生任何空(全零)列.要添加这些内容,最简单的方法是:

Note that this does not produce any of the empty (all-zero) columns. To add those, it is simplest to do:

out2 = zeros(N,N);
out2(:,1:size(out,2)) = out;

这篇关于如何在Matlab中使用信号样本的移位版本创建矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆