# Make a loop to find each first positive value after a negative value in an vector.

20 views (last 30 days)
Lois Slangen on 22 Jul 2019
Answered: Mario Chiappelli on 22 Jul 2019
HI,
I have a vector with about 2000 positive and negative values. I want to make a loop that finds the first positive point/value after the negative values.
Once the loop has found that point, check if more than 75% of all values after that point are positive.
If yes, save that point into point_found. If not, continue the loop to search for the next positive value/point in the vector.
Once the loop has again found that point, check if from there on, more than 75% of all values after that point are positive.
If yes, save that point into point_found. If not, continue the loop to search for the next positive value/point in the vector. and so on and on until the point/value is found.
If there is no such point or value put into point_found=9999
Lois Slangen on 22 Jul 2019
list is the name of my vector

Mario Chiappelli on 22 Jul 2019
Try this out:
numbers = [1,2,3,-1,2,3,4,5,6,-7,-8,10,4,64,12,12,432,221,12];
num = length(numbers);
negativeCheck = 0;
for i = 1:num
if negativeCheck == 1
disp(calculatePercent(i,num,numbers));
if numbers(i) > 0 && calculatePercent(i,num,numbers) >= .75
pointOfInterest(i) = numbers(i);
pointOfInterestIndex(i) = i;
negativeCheck = 0;
end
end
if numbers(i) < 0
negativeCheck = 1;
end
end
pointOfInterest = nonzeros(pointOfInterest');
pointOfInterestIndex = nonzeros(pointOfInterestIndex');
function percent = calculatePercent(minValue, maxValue, list)
checkerList = double(maxValue-minValue);
for i = minValue:maxValue
if list(i) >= 0
checkerList(i) = 1;
else
checkerList(i) = 0;
end
end
percent = mean(checkerList);
end
I added a list that creates an index of which values are stored so you know their position from the original vector.