sum of an array and checking condition
显示 更早的评论
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..
采纳的回答
更多回答(2 个)
Ahmet Cecen
2014-8-3
checksum = 0;
I=0;
while checksum<=limit
I=I+1;
checksum=checksum+yourarray(I);
end
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)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!