如何在满足if条件的情况下跳转到for循环中的特定位置?
How to jump to a particular place in the for loop if the if condition is satisfied?
我有一些图像文件。我正在尝试使用每个文件执行一些计算,如果满足特定条件,我想返回到代码中的特定行并从那里再次 运行 它。但也只是再一次。不管第二次if条件满足还是不满足,我都想进行下一次迭代。但是,MATLAB 似乎没有 goto 函数,而且使用 goto 意味着糟糕的编程,所以我想我只是为满足 if 条件的特定 'i' 值迭代 for 循环两次。
file = dir('*.jpg');
n = length(file);
for i = 1:n
*perform some operations on the 'i'th file*
if 'condition'
*run the for loop again for the 'i'th file instead of going to the 'i+1'th file*
i=i-1;
else
*go to next iteration*
end
end
我试图通过将循环内的循环变量 'i' 更改为 'i-1' 来编写代码,这样在下一次迭代中,第 'i' 循环将再次重复但是这样做会给出错误的输出,尽管我不知道我的代码中是否存在其他错误,或者内部循环变量的更改是否是问题的原因。感谢您对此提供任何帮助。
将 for
循环替换为 while
循环以获得更大的灵活性。唯一的区别是您必须手动递增 i
,因此这也允许您不递增 i
.
根据您的新要求,您可以跟踪尝试次数,并在需要时轻松更改:
file = dir('*.jpg');
n = length(file);
i = 1;
attempts = 1;
while i <= n
% perform code on i'th file
success = doSomething(); % set success true or false;
if success
% increment to go to next file
i = i + 1;
elseif ~success && attempts <= 2 % failed, but gave it only one try
% increment number of attempts, to prevent performing
attempts = attempts + 1;
else % failed, and max attempts reached, increment to go to next file
i = i + 1;
% reset number of attempts
attempts = 1;
end
end
考虑到在 之后添加的新要求,最简单的方法是在单独的函数中将代码从循环中分离出来:
function main_function
file = dir('*.jpg');
n = length(file);
for i = 1:n
some_operations(i);
if 'condition'
some_operations(i);
end
end
function some_operations(i)
% Here you can access file(i), since this function has access to the variables defined in main_function
*perform some operations on the 'i'th file*
end
end % This one is important, it makes some_operations part of main_function
我有一些图像文件。我正在尝试使用每个文件执行一些计算,如果满足特定条件,我想返回到代码中的特定行并从那里再次 运行 它。但也只是再一次。不管第二次if条件满足还是不满足,我都想进行下一次迭代。但是,MATLAB 似乎没有 goto 函数,而且使用 goto 意味着糟糕的编程,所以我想我只是为满足 if 条件的特定 'i' 值迭代 for 循环两次。
file = dir('*.jpg');
n = length(file);
for i = 1:n
*perform some operations on the 'i'th file*
if 'condition'
*run the for loop again for the 'i'th file instead of going to the 'i+1'th file*
i=i-1;
else
*go to next iteration*
end
end
我试图通过将循环内的循环变量 'i' 更改为 'i-1' 来编写代码,这样在下一次迭代中,第 'i' 循环将再次重复但是这样做会给出错误的输出,尽管我不知道我的代码中是否存在其他错误,或者内部循环变量的更改是否是问题的原因。感谢您对此提供任何帮助。
将 for
循环替换为 while
循环以获得更大的灵活性。唯一的区别是您必须手动递增 i
,因此这也允许您不递增 i
.
根据您的新要求,您可以跟踪尝试次数,并在需要时轻松更改:
file = dir('*.jpg');
n = length(file);
i = 1;
attempts = 1;
while i <= n
% perform code on i'th file
success = doSomething(); % set success true or false;
if success
% increment to go to next file
i = i + 1;
elseif ~success && attempts <= 2 % failed, but gave it only one try
% increment number of attempts, to prevent performing
attempts = attempts + 1;
else % failed, and max attempts reached, increment to go to next file
i = i + 1;
% reset number of attempts
attempts = 1;
end
end
考虑到在
function main_function
file = dir('*.jpg');
n = length(file);
for i = 1:n
some_operations(i);
if 'condition'
some_operations(i);
end
end
function some_operations(i)
% Here you can access file(i), since this function has access to the variables defined in main_function
*perform some operations on the 'i'th file*
end
end % This one is important, it makes some_operations part of main_function