Subtract element one from element two in a 1x101 row vector.

8 ビュー (過去 30 日間)
Richard Honan
Richard Honan 2021 年 8 月 2 日
編集済み: darova 2021 年 8 月 2 日
I need to subtract the first element of a vector from the second element. Then continue in that manner.
E3 - E2 then E4 - E3 then E5 - E6 to the end.
% Calculate the Velocity and Accelleration for particle 1 at all times.
load A1_input.txt;
t=A1_input(:,1)'; % Time in Seconds.
% Calculate the Time for particle using loops.
for i=1:length(t)
Time(i) =(t(:,2))-(t(:,1));
end
Time(36) % Trial ans wrong

回答 (1 件)

Mohammad Sami
Mohammad Sami 2021 年 8 月 2 日
編集済み: Mohammad Sami 2021 年 8 月 2 日
You can use the subset from 1:end-1 and 2:end to calculate without using the for loop.
t = rand(1,101);
tsub = t(2:end) - t(1:end-1)
tsub = 1×100
-0.4849 0.1134 0.1850 -0.3043 -0.2828 0.1766 -0.0752 0.1635 0.2096 -0.4114 0.5760 0.1126 -0.7018 -0.0319 0.0563 0.3502 0.3789 -0.4994 -0.0447 -0.2081 0.2137 0.1894 0.3079 -0.0553 -0.7959 0.0219 0.5112 -0.5026 0.1934 -0.1884
  1 件のコメント
Richard Honan
Richard Honan 2021 年 8 月 2 日
That works well but the requirement says to use loops.

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

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by