Subtract two vectors while shifting them along

3 ビュー (過去 30 日間)
cles
cles 2018 年 1 月 10 日
コメント済み: cles 2018 年 1 月 19 日
Suppose we have two vectors
A = [1 2 3 4];
B = [4 3 2 1];
My goal is to shift them along and get the differences for each position. So for the first combination
[1 2 3 4]
-[4 3 2 1]
the result would be:
C = [0] % difference of 1 and 1
For the second:
C = [-1 1] % differences of 1-2 and 2-1
For the third:
C = [-2 0 -2]
and so on.
I'm stuck on programming a function that works for two vectors of arbitrary sizes, especially how to handle the leading and lagging numbers. For further processing, it would be great to be able to assign NaN to those.
Thanks in advance.

採用された回答

Aveek Podder
Aveek Podder 2018 年 1 月 17 日
編集済み: Aveek Podder 2018 年 1 月 17 日
Hi,
You can use length command of Matlab to get the size of the array and write a for loop and proper indexing of the array to get the C vector done. Please have a look at the code below.
L_A = length(A)
L_B = length(B)
for i = 1:min(L_A,L_B)
C = A(1:i) - B((L_B-i+1):L_B)
end
  1 件のコメント
cles
cles 2018 年 1 月 19 日
This worked out, together with using some stuff I found in the implementation of the xcorr function

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by