sum of an array and checking condition
2 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have an array (1,10) which contains 10 values..now I have to sum the values of those array element and shall check whether the sum cross the limits or not. if it cross the limits then I need to know at which elements the conditions fails..
so if my array contains 2 2 2 1 2 2 1 2 2 2 then starting from my first elements of array, i shall sum up and shall check whether sum cross 6 or not..here at 4th element sum exceeds I need those index 4.
I tried but my logic jumbled up..
0 件のコメント
採用された回答
RS
2014 年 8 月 3 日
if v be your array having your values then
for i=1:10
if (sum(v(1:i))>6)
i
break
end
end
it would show your desired value 4
thanks
0 件のコメント
その他の回答 (2 件)
Ahmet Cecen
2014 年 8 月 3 日
checksum = 0;
I=0;
while checksum<=limit
I=I+1;
checksum=checksum+yourarray(I);
end
0 件のコメント
Azzi Abdelmalek
2014 年 8 月 3 日
You can use cumsum function
v=[2 2 2 1 2 2 1 2 2 2];
idx=find(cumsum(v)>6,1)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!