row vector summation in pairs without looping

1 回表示 (過去 30 日間)
Sri Ramam Somanchi
Sri Ramam Somanchi 2023 年 6 月 17 日
回答済み: Satwik Samayamantry 2023 年 6 月 17 日
Hi, I'm a beginner in matlab,I need to compute the sum of all consecutive pairs in my row vector and sum of all those sums computed without looping over.
Ex : [1 2 3 4 5] will give (1+2)+(2+3)+(3+4)+(4+5)
Hope you can help me out. Thanks in advance!

採用された回答

Satwik Samayamantry
Satwik Samayamantry 2023 年 6 月 17 日
Considering you are giving a row vector as input named inputVec, you can get the required result using the following line without for loop
result = 2*sum(inputVec)-inputVec(1)-inputVec(length(inputVec))

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2023 年 6 月 17 日
Seems like you could create 2 vectors from the given vector, one containing the first number in each summation, and the other containing the second number, and then just add the 2 vectors together.

カテゴリ

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

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by