移动行矢量值

时间:2015-04-05 13:42:44

标签: matlab

如何像这样移动向量c的条目:

c = [1; 2; 0];
x = [1; 2; 3];

y(1:3) = rightshift(c', 0:2) * x;

% Should produce:
% y(1) = [1; 2; 0]' * x; 
% y(2) = [0; 1; 2]' * x; 
% y(3) = [0; 0; 1]' * x;

生成输出向量y

2 个答案:

答案 0 :(得分:2)

使用toeplitz -

N = numel(c);
y = triu(toeplitz(c,0:N-1).')*x

mod & bsxfun -

N = numel(c)
y = triu(c((mod(bsxfun(@minus,[0:N-1]',0:N-1),N)+1).'))*x

或仅使用bsxfun -

N = numel(c);
c_ext = [zeros(N-1,1) ; c(:)]
y = c_ext(bsxfun(@plus,[N:-1:1]',[0:N-1]))*x

答案 1 :(得分:1)

这基本上是一个卷积:

y = conv(c(end:-1:1), x);
y = y(end-numel(c)+1:end);