Make a loop to find each first positive value after a negative value in an vector.
4 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Mario Chiappelli
2019-7-22
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.
0 个评论
更多回答(0 个)
另请参阅
类别
在 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!