how to do sum of element of multiple number in matlab?
2 ビュー (過去 30 日間)
古いコメントを表示
Putri Basenda Tarigan
2020 年 11 月 18 日
コメント済み: Putri Basenda Tarigan
2020 年 11 月 18 日
Hello everyone.
If I have
m=[3 3 4 3 2 4 3]
c=[3 4]
then I want to calculate the sum of m(c) at the same time
I tried
b=1:m(c)
b=sum(b)
but only give me one number. I want it to be 2 numbers. How to do this?
Thanks in advance
2 件のコメント
Timo Dietz
2020 年 11 月 18 日
Could you please explain in more detail? Does c contain the indices up to which you like to sum up m - starting always from 1? Or why do you expect two numbers as result?
採用された回答
その他の回答 (1 件)
Timo Dietz
2020 年 11 月 18 日
編集済み: Timo Dietz
2020 年 11 月 18 日
Not sure whether I got your issue. But in case you want to summarize all members of m starting at index 1, up to each member of c (containing indices of m) individually, you can do this:
b=arrayfun(@(x) sum(m(1:x)), c)
2 件のコメント
Rik
2020 年 11 月 18 日
Functions like arrayfun and cellfun tend to be slower than using a for loop. The exception appears to be the legacy calls of cellfun (so those with the char input, instead of a function handle).
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!