필터 지우기
필터 지우기

Recursive function not stopping

조회 수: 2 (최근 30일)
Nagesh A P
Nagesh A P 2018년 6월 18일
댓글: Dennis 2018년 6월 18일
I'm running the following recursive function. By pausing the execution and checking the values, I find that the execution is happening and values are updated. I have a stopping condition and the workspace is even showing me the final matrix once I pause after sometime. But it's not exiting the function after doing that. It keeps on running. I'm not able to figure out why. I call the function using x_1=no_vel_miss(a);
function r = no_vel_miss( x ) %x is my matrix with all data-time and vel as columns
i=1;
flag=0;
flag=sum(isnan(x(:,2)));
if flag==0
r=x; %if there is no missing point, return the matrix
else
while i<=length(x(:,1))
if isnan(x(i,2))
for j=i:-1:1
if x(j,2)==0
prezero=j; %prezero is the nearest zero velocity before missing point
break;
end
end
for j=i:length(x(:,1))
if x(j,2)==0&&x(j+1,2)~=0
postzero=j; %postzero is the nearest zero velocity after which a non-zero velocity occurs
break;
end
end
x_1=cat(1,x(1:prezero,:),x(postzero:length(x(:,1)),:)); %here I'm removing all that data near the missing point as mentioned above
i=prezero;
r=no_vel_miss(x_1);
else
i=i+1;
end
end
end
end
  댓글 수: 8
Nagesh A P
Nagesh A P 2018년 6월 18일
@Geoff Hayes, If my input is transpose[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20;0 1 3 4 nan 0 1 2 5 2 0 nan 4 5 nan 0 0 3 6 0] with time and velocity representing the 2 columns respectively, my output should be transpose[1 5 6 7 8 9 10 15 16 17 18 19 20 ;0 0 1 2 5 2 0 0 0 3 6 0] @Dennis, each time the function is called, a different matrix is sent as input, so starting from prezero won't create a problem.
Dennis
Dennis 2018년 6월 18일
Yes, a new function is called, but the 'old' function does not simply stop

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

답변 (1개)

OCDER
OCDER 2018년 6월 18일
Is this what you want to do?
%
% x = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20;
% 0 1 3 4 NaN 0 1 2 5 2 0 NaN 4 5 NaN 0 0 3 6 0]';
%
% x = no_vel_miss(x)
%
% x' =
% 1 6 7 8 9 10 11 16 17 18 19 20
% 0 0 1 2 5 2 0 0 0 3 6 0
function x = no_vel_miss( x )
NanIdx = find(isnan(x(:, 2)));
if isempty(NanIdx)
return
end
DelLoc = zeros(size(x, 1), 1, 'logical');
for k = 1:length(NanIdx)
PreZero = find(x(1:NanIdx(k)-1, 2) == 0, 1, 'last');
if isempty(PreZero)
PreZero = 1;
end
PostZero = find(x(NanIdx(k)+1:end, 2) == 0, 1) + NanIdx(k);
if isempty(PostZero)
PostZero = size(x, 1);
end
DelLoc(PreZero+1:PostZero-1) = 1;
end
x(DelLoc, :) = [];

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by