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

17 visualizaciones (últimos 30 días)
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 comentarios
Lois Slangen
Lois Slangen el 22 de Jul. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Mario Chiappelli
Mario Chiappelli el 22 de Jul. de 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.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by