Calculating the difference between a vector and time

2 ビュー (過去 30 日間)
Nikolaos Zafirakis
Nikolaos Zafirakis 2019 年 5 月 4 日
コメント済み: Star Strider 2019 年 5 月 5 日
I'm looking for a way to get this loop to work, basically I need to calculate the difference in a 3x1 vector divide by the difference in time. Does anyone have any suggestion?
t = 2000
n= 0;
for ind = 0:1:t
n= n+ind;
S(n) = (diff(x( don't know how to input the value here )))./diff(t(ind)); %x is a three element vector
end
  5 件のコメント
Nikolaos Zafirakis
Nikolaos Zafirakis 2019 年 5 月 5 日
So, the diff command wont work for this because if
A = [1 1 2 3]
diff(A)
= [ 0 1 1 2]
where I would need the ouput to be
=[1 0 1 1]
because
Delta_A(1) = 1 - 0 = 1 % first element - no element
Delta_A(2) = 1 - 1 = 0 % second element - first element
Delta_A(3) = 2 - 1 = 1 % third element - second element
Delta_A(4) = 3 - 2 = 1 % four element - third element
Star Strider
Star Strider 2019 年 5 月 5 日
If you are ‘padding’ ‘A’ with an initial 0, you need to tell MATLAB.
MATLAB does many things well, although it is sadly deficient when it comes to mind-reading.
Try this:
A = [1 1 2 3];
dA = diff([0 A])
producing:
dA =
1 0 1 1
NOTE — You need to do similar ‘padding’ for your time vector.

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

回答 (1 件)

Omer N
Omer N 2019 年 5 月 5 日
Try this:
A = [1 1 2 3];
A-[0,A(1:end-1)]
ans =
1 0 1 1

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by