Recursive function not stopping

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 件のコメント

Ameer Hamza
Ameer Hamza 2018 年 6 月 18 日
What is the termination condition i.e. how do you decide that the loop recursion should finish?
Geoff Hayes
Geoff Hayes 2018 年 6 月 18 日
Nagesh - please provide a sample set of data that you are using. When I run your code I get errors because prezero and postzero are not defined. When I initialize with dummy values, I noticed that the input to no_vel_miss (which is x_1) has one more row than the initial input x to this function...so the recursive call is working on a data set that is larger than the initial input. Which, in my case, leads to a MATLAB error/warning of my maximum recursion limit being met.
Dennis
Dennis 2018 年 6 月 18 日
I think it might be your while loop that runs forever. If x(i,2) is NaN for any i, prezero will be equal or smaller than i. Later you set i to prezero so your loop will find the exact same NaN value again and repeat.
Nagesh A P
Nagesh A P 2018 年 6 月 18 日
@Ameer Hamza, the loop should terminate when flag=0, i.e., no NaNs in the matrix x. @Geoff Hayes, the real data I'm using is a velocity -time data. The sample data can be a square wave with breaks in it, ie., some data missing. I want to remove the square which has missing data and the zeros in between this square and the next square. @Dennis every time the next iteration starts, the input matrix itself is changing. The nans associated with a particular prezero are deleted.
Geoff Hayes
Geoff Hayes 2018 年 6 月 18 日
Nagesh - can you provide an example input that exhibits the behaviour that you are observing? have you confirmed that the input to the recursive function is a subset of the data to the outer call?
Dennis
Dennis 2018 年 6 月 18 日
i might be missing something...but where?
while i<=length(x(:,1))
if isnan(x(i,2)) %assume this is true for i=5
for j=i:-1:1 %runs from 5 to 1
if x(j,2)==0 %might be true for j=4
prezero=j; %prezero=4
break; %we leave for loop
end
end
for j=i:length(x(:,1)) %runs from 5 to whatever
if x(j,2)==0&&x(j+1,2)~=0 %needs to be true for same value else you get an error
postzero=j; %postzero gets a value
break; %we leave another for loop
end
end
x_1=cat(1,x(1:prezero,:),x(postzero:length(x(:,1)),:)); %we create x_1 - not sure what this is though
i=prezero; %i is set to 4 in this example
r=no_vel_miss(x_1); %we call a recursive function and get r
%after this while repeats with i=4
%will enter else ->i=i+1;
%now i is 5 where did you change your matrix?
else
i=i+1;
end
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 日

0 投票

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, :) = [];

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

質問済み:

2018 年 6 月 18 日

コメント済み:

2018 年 6 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by