Recursive function not stopping

4 vues (au cours des 30 derniers jours)
Nagesh A P
Nagesh A P le 18 Juin 2018
Commenté : Dennis le 18 Juin 2018
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 commentaires
Nagesh A P
Nagesh A P le 18 Juin 2018
@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 le 18 Juin 2018
Yes, a new function is called, but the 'old' function does not simply stop

Connectez-vous pour commenter.

Réponses (1)

OCDER
OCDER le 18 Juin 2018
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, :) = [];

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by