你的伪代码对我来说有点太强烈了——我试图重构你想要做的事情。如果我理解正确,这应该可以解决您的问题(将计算的中间结果存储在数组 Z 中):
L=100
z = zeros(L,10);
for x=1:10
% perform some calculations
eqn1
for y=1:L
% perform some more calculations; it is not clear whether the result of
% this loop over y=1:L yields one value, or L. I am going to assume L values
z(y, x) = eqn2(x, y)
end
value =num2strn eqn2
disp value
end
% now you have the value of each evaluation of the innermost loop available. You can plot it as follows:
figure;
plot( x, z); % multiple plots with a common x parameter; may need to use Z' (transpose)...
title 'this is my plot';
xlabel 'this is the x axis';
ylabel 'this is the y axis';
至于您在评论中提出的其他问题,您可能会从以下内容中找到灵感:
L = 100;
nx = 20; ny = 99; % I am choosing how many x and y values to test
Z = zeros(ny, nx); % allocate space for the results
x = linspace(0, 10, nx); % x and y don't need to be integers
y = linspace(1, L, ny);
myFlag = 0; % flag can be used for breaking out of both loops
for xi = 1:nx % xi and yi are integers
for yi = 1:ny
% evaluate "some function" of x(xi) and y(yi)
% note that these are not constrained to be integers
Z(yi, xi) = (x(xi)-4).^2 + 3*(y(yi)-5).^2+2;
% the break condition you were asking for
if Z(yi, xi) < 5
fprintf(1, 'Z less than 5 with x=%.1f and y=%.1f\n', x(xi), y(yi));
myFlag = 1; % set flag so we break out of both loops
break
end
end
if myFlag==1, break; end % break out of the outer loop as well
end
这可能不是您的想法 - 我无法理解“运行循环直到 z(y,x) <5 的所有值,然后它应该输出 x”。如果你运行外循环完成(这是你知道“z(y,x)的所有值”的唯一方法,那么你的 x 值将是它的最后一个值......这就是我建议运行的原因x 和 y 的所有值,收集整个矩阵 Z,然后检查 Z 以获得您想要的东西。
例如,如果您想知道是否存在所有 Z < 5 的 X 值,您可以这样做(如果您没有跳出for
循环):
highestZ = max(Z, [], 1); % "take the highest value of Z in the 1 dimension
fprintf(1, 'Z is always < 5 for x = %d\n', x(highestZ<5));
等等
如果你不能从这里弄清楚,我放弃......