looping till a condition is met.....

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

 Accepted Answer

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 Comments

Hi i got what i was expecting.
Need a small step; want the values of the work until they break into different arrays.so i have arrays with raw data until they break. Tried this but didn't work
Values(1,chunk) = cdf (1:limitLocation);
If you want the component values that go into making up the sum, this could be a different number each time. So that means you need a cell array instead of a rectangular numerical array like you tried. So to save the values, do this:
Values{chunk} = Work(index:(index + limitLocation - 1));
This cell will be an array of 3 elements sometimes, 4 at other times, or maybe more. It is whatever elements went into creating your sum of approximately 2.
Hi; I need a small help with the sorted values. Once the chunks are separated based on values I want to check each chunk for a condition and if that condition is not met leave the chunk and go to next chunk and check for the the condition again.
So for suppose i have 60 chunks first I want to check the values in each chunk for a condition (if 1 value in the chuck doesn't meet the condition the whole chunk is invalid) and if the condition is not met leave the chunk. So at the end i only want the chunks in which the values meet the desired condition.
Make a little function to text the chunk for a condition, then put into a loop
for k = 1 : length(Values)
thisArray = Values{k}; % Variable number of elements
metCondition(k) = CheckChunkCondition(thisArray); % Returns true or false
end
% Extract only those chunks meeting condition:
Values = Values(metCondition);
sri satya ravi
sri satya ravi on 8 Jan 2017
Edited: sri satya ravi on 8 Jan 2017
for k = 1 : length(Values) thisArray = Values{k}; % Variable number of elements metCondition(k) = Window_EXH_temp{chunk}<200(thisArray); % Returns true or false end % Extract only those chunks meeting condition: Values = Values(metCondition);
Hi; For my dataset i got Window_EXH_temp as 1*3518. each column has different size dataset again. so i wanted to check for condition when the value in Window_EXH_temp in a chunk is less than 200 exclude that whole chunk and move on to the next one. how should i write the condition.
If you want me to attach my whole code I can do that.
Thanks a lot for the help
Try this:
function metCondition = CheckChunkCondition(vector)
metCondition = true; % Initialize
if any(vector < 200)
metCondition = false;
end

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 27 Dec 2016
Edited: the cyclist on 27 Dec 2016
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 Comments

is it not checking against zeros as W is an array of zeros...?? if W(idx) >= limit
Did you actually try to run the code and see how it works?
At first, W(idx) = 0, true. But when that is the case, Work(i) is added to W(idx) and it is not zero anymore. That same element W(idx) will then be checked again. It's not zero anymore, and it might be over the limit now.
Thanks..... I did try it and it worked. W is showing an array of 36 numbers. So my data is split into 36 segments of values close to 2. Can i know if there is a way to find the point at which the condition is met...i.e first 20 values till the condition is met or 25 values like that.
I've edited the code to track that in the variable C.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!