how to extract a value from a sum in MATLAB

2 ビュー (過去 30 日間)
Jessica Shallop
Jessica Shallop 2013 年 3 月 27 日
i am trying to extract a certain value from a sum. for example, i have this 1+2+3+4+5 and from that i want to extract 2+3 how can this be done in MATLAB. This may seem like a basic question but I am new to MATLAB and have no idea what to do. I couldn't find anything on google or the MATLAB help

採用された回答

Wayne King
Wayne King 2013 年 3 月 27 日
編集済み: Wayne King 2013 年 3 月 27 日
Do you always know the position of the terms you want to extract? Presumably, you have a vector in MATLAB and you are summing the elements of that vector, so you can just do a partial sum by using those indices.
x = [1 2 3 4 5];
sum(x(2:3))
If it's a very big vector and you're not sure the location of those elements, you can use logical indexing.
sum(x(x>1 & x<4))
The above is simply saying, look inside x for all elements greater than 1 and less than 4 (and sum those)
Or is it more complicated than that?
  3 件のコメント
Wayne King
Wayne King 2013 年 3 月 27 日
You can't extract 2+3 in that precise form if that's what you're asking. are you saying you want the elements?
y = x(x>1 & x<4);
will give you
y = [2 3]
Jessica Shallop
Jessica Shallop 2013 年 3 月 27 日
yes that will be fine.. thank you very much

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by