looping till a condition is met.....
조회 수: 14 (최근 30일)
이전 댓글 표시
Hi;
I have an array of 2400 values. I want to loop such that i need to break into chunks till the sum becomes (say for suppose 2). i want to add 1st value plus 2nd value so on till the condition is met and then after the condition is met start adding from next number again till the condition meets.
I need chunks of values whose sum is 2 and i don't know the the exact chunks which vary by the data and the limit i set for the condition to break.
limit = 2;
s = 0;
for i = 1:length(Work)
while 1
if s >= limit
break
end
s = s + Work(i);
end
W = Work(i)
end
댓글 수: 0
채택된 답변
Image Analyst
2016년 12월 28일
I believe this will do what you want. Give it a try. It scans along work getting the cumulative sum from that index onwards. Then it finds the first index where that cumulative sum exceeds 2. When that happens, it records the sum for that chunk (typically in the range 2-3) and the original index where that chunk ends.
% Create sample data.
Work = rand(2400,1);
% Define the limit of sums that the chunks need to surpass.
sumLimit = 2;
% Preallocate more memory than needed for W
sumValues = zeros(size(Work));
indexLocations = zeros(size(Work));
chunk = 1; % Index for what chunk we're working on.
index = 1;
while index < length(Work) && chunk < length(Work)
% Find the sums from this index onwards to the end of the Work vector.
cdf = cumsum(Work(index:end));
% Find the first time the sum exceeds the limit.
limitLocation = find(cdf > sumLimit, 1, 'first');
% Remember, limitLocation is relative to Index, not to element 1.
% If no remaining sum is more than sumLimit, just break out of the loop.
if isempty(limitLocation)
break;
end
% Save this sum value.
sumValues(chunk) = cdf(limitLocation);
% Save this index value. The index value relative to index 1, not relative to limitLocation.
indexLocations(chunk) = limitLocation + index - 1;
% Move pointer to the next element.
index = indexLocations(chunk) + 1;
% Increment what chunk number we're working on.
chunk = chunk + 1;
end
% Trim trailing zeros from sumValues and indexLocations.
sumValues(sumValues == 0) = []
indexLocations(indexLocations == 0) = []
댓글 수: 6
Image Analyst
2017년 1월 8일
Try this:
function metCondition = CheckChunkCondition(vector)
metCondition = true; % Initialize
if any(vector < 200)
metCondition = false;
end
추가 답변 (1개)
the cyclist
2016년 12월 27일
편집: the cyclist
2016년 12월 27일
Here's one way. The core idea is that one tests whether the current value of W is over the limit, and if so then move the "pointer" index to the next element.
% Some pretend data
Work = rand(2400,1);
limit = 2;
% Preallocate
W = zeros(size(Work));
C = zeros(size(Work));
idx = 1;
for i = 1:length(Work)
C(idx) = C(idx) + 1;
if W(idx) >= limit
idx = idx + 1;
end
W(idx) = W(idx) + Work(i);
end
% Trim the excess from W and C
W(idx+1:end) = [];
C(idx+1:end) = [];
댓글 수: 4
참고 항목
카테고리
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!