Looping through numbers in file and adding first two numbers then next two and so on

1 回表示 (過去 30 日間)
Yenifer Ramirez
Yenifer Ramirez 2019 年 8 月 1 日
編集済み: Baha411 2019 年 8 月 2 日
A = [1 2 3 4 5]
1+2 = 3
2+3 = 5
3+4 = 7
4+5 = 9
How can I loop through an array of numbers like above while storing the number everytime numbers are added? Thank you for any help!
  1 件のコメント
Baha411
Baha411 2019 年 8 月 1 日
編集済み: Baha411 2019 年 8 月 2 日
you can do sth like this:
B = [A 0]+[0 A];
B([1 end]) = [];
B
B =
3 5 7 9

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

回答 (2 件)

the cyclist
the cyclist 2019 年 8 月 1 日
A(1:end-1) + A(2:end)

Andrei Bobrov
Andrei Bobrov 2019 年 8 月 2 日
編集済み: Andrei Bobrov 2019 年 8 月 2 日
conv(A,[1,1],'valid')
or
b = movsum(A,2);
out = b(2:end);

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by