Problem: Assuming you have n=[16,18,23,24,39,40] values, how would you create a funtionality in Matlab to output the number of values needed to make a sum of 100, every time you call out a random value amongst them.
To eplain further: Assume it's a dart board, and the question is, how many darts do I need to throw and hit any number and after a number of throws the hit numbers add up to a 100?
I'm new to Matlab but I would solve the same with C/C++. Any insights are highly appreciated.

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 4 月 19 日
編集済み: Azzi Abdelmalek 2013 年 4 月 19 日

0 投票

n=[16,18,23,24,39,40]
find(cumsum(n)>=100,1)
%or
a=0;
k=1
while a<100 & k<numel(n)
a=a+n(k);
k=k+1;
end
out=k-1

カテゴリ

ヘルプ センター および File Exchange で Programming についてさらに検索

質問済み:

2013 年 4 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by