How to add consecutive numbers from a column in a .txt file?

8 ビュー (過去 30 日間)
Andrew Lackey
Andrew Lackey 2021 年 9 月 2 日
コメント済み: Wan Ji 2021 年 9 月 2 日
I am trying to add consecutive elements in a column and then print the new column. For example the column:
[1 1 8 2 3 1]
Would print:
[2 9 10 5 4]
I already have:
fid = fopen('HW1.txt', 'r');
data = textscan(fid, '%d%f%s%s%s%d%s', 'MultipleDelimsAsOne',true, 'HeaderLines',1);
fclose(fid);
jumps = sum(abs(diff(data{1})>1));
timeD = diff(data{2});
opts = detectImportOptions('HW1.txt');
preview('HW1.txt',opts)
[Var1,Var2,Var3,Var4] = readvars('HW1');
whos Var1 Var2 Var3 Var4
-----------------------------------
Var 4 is a column of 200 numbers that I would like to convert into a column where each number is the added value of the columns adjacent pair.

採用された回答

Wan Ji
Wan Ji 2021 年 9 月 2 日
編集済み: Wan Ji 2021 年 9 月 2 日
Just use movsum
a = [1 1 8 2 3 1];
b = movsum(a,2); % I should apologize for not adding 2
b = b(2:end)
The answer
b =
2 9 10 5 4
  5 件のコメント
Wan Ji
Wan Ji 2021 年 9 月 2 日
I am sorry for not adding 2 in the input variable of function movsum
a = [1 1 8 2 3 1]';
b = movsum(a,2); % I should apologize for not adding 2
b = b(2:end)
The result
b =
2
9
10
5
4
Wan Ji
Wan Ji 2021 年 9 月 2 日

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by