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

조회 수: 1 (최근 30일)
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 CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by