필터 지우기
필터 지우기

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

조회 수: 11 (최근 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
  댓글 수: 5
Lois Slangen
Lois Slangen 2019년 7월 22일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Mario Chiappelli
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개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by