summation of diffrent lengths in vector

I would like to sum some lengths in vector. For example I have a=[0.0 0.0 0.3 0.4 0.5 0.0 0.0 0.0 0.0 1.0 1.1]. I need b=((0.5-0.3)+(1.1-1.0))=0.3. The time step is constant=0.1 and this order of elements can be change in new run of the program.Thanks alot.

1 件のコメント

Grzegorz Knor
Grzegorz Knor 2011 年 10 月 12 日
According to what rules you want to sum up? Could you explain?

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

 採用された回答

Clemens
Clemens 2011 年 10 月 12 日

0 投票

I would do this with some logical indexing magic.
a=[0.0 0.0 0.3 0.4 0.5 0.0 0.0 0.0 0.0 1.0 1.1];
step = 0.1;
tmp = a>0; % rule what elements to take
i_sum = tmp(1:end-1) & tmp(2:end)>0; % magic removes 1 element from each group
distances = sum((i_sum))* step % = 0.3

1 件のコメント

Sakhr
Sakhr 2011 年 10 月 12 日
It works fine,really magic, thanks for help

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2011 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by