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

21 vues (au cours des 30 derniers jours)
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
  5 commentaires
Lois Slangen
Lois Slangen le 22 Juil 2019
this is what I have have so far, but it doesn't work at all.
for i=1:length(list)
if list (i) >= 0 && list (i-1)<=0
point (i) = i;
end
if mean(list(point_found:end) >= 0) >= 0.75
point_found = point(i);
end
end
Lois Slangen
Lois Slangen le 22 Juil 2019
list is the name of my vector

Connectez-vous pour commenter.

Réponse acceptée

Mario Chiappelli
Mario Chiappelli le 22 Juil 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.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by