我有以下代码段:
function [S,r1,r2,c1,c2] = xor2imgs(org,templete,Diff)
while (r+O - 1) <= N
while (c+P - 1) <= M
if sum_temp <= sum(sum(sum(org(r:r+O-1,c:c+P-1)))) %cond #1
matrix = org;
matrix(r:r+O-1,c:c+P -1) = org(r:r+O-1,c:c+P-1)-templete(1:O,1:P);
mat_sum = sum(sum(sum(matrix)));
diff2 = org_sum-mat_sum;
if (mat_sum == diff) && (diff2 > Diff) %cond #2
S = org_sum-mat_sum;
r1 = r;
r2 = r1+O-1;
c1 = c;
c2 = c1+P-1;
return ; %must return to the original function yet keep the output values
end
end
end
end
end
使用return后,我应该怎么做才能将[S,r1,r2,c1,c2]的值传递给调用方函数? 我将函数调用为
[S,r1,r2,c1,c2] = xor2imgs(org,newtemplete,S);
在另一个函数中,但它每次输出始终返回零。...
答案 0 :(得分:0)
我假设所有变量都在while循环上方定义...
您 可以 使用多个break语句。之所以需要多个break语句,是因为您有嵌套循环,而一个break语句就不够用。我在您的代码中添加了一个计数器变量,以在您需要跳出循环时更改为1:
counter = 0;
function [S,r1,r2,c1,c2] = xor2imgs(org,templete,Diff)
while (r+O - 1) <= N
while (c+P - 1) <= M
if sum_temp <= sum(sum(sum(org(r:r+O-1,c:c+P-1)))) %cond #1
matrix = org;
matrix(r:r+O-1,c:c+P -1) = org(r:r+O-1,c:c+P-1)-templete(1:O,1:P);
mat_sum = sum(sum(sum(matrix)));
diff2 = org_sum-mat_sum;
if (mat_sum == diff) && (diff2 > Diff) %cond #2
S = org_sum-mat_sum;
r1 = r;
r2 = r1+O-1;
c1 = c;
c2 = c1+P-1;
counter = 1;
break;
end
end
end
if isequal(counter,1)
break;
end
end
end