Capturing outputs from Loops

1 回表示 (過去 30 日間)
Joe Bannen
Joe Bannen 2016 年 10 月 25 日
コメント済み: Steven Lord 2016 年 10 月 25 日
Hi
I have produced some code but now need to look at how it performs under various changes in input. This (I hope!) is best done by using loops. Capturing the various outputs and plotting them is where I am stuck!
for n=1:0.1:10
% Various Functions
error1=
error2=
end
error1 and error2 are produced and work well in isolation. I need to vary n and capture error1 and error2 for each n. Plotting error1 and error2 against n would be the end target.
I can't quite see how to do this!
Any help in how to do this would be very welcome. How to measure and capture the tic/toc for each n would be useful as well.
Cheers
Joe

採用された回答

Alexandra Harkai
Alexandra Harkai 2016 年 10 月 25 日
To keep track of error1/2 and times, you can maintain an array for each of them:
error1 = zeros(91,1);
error2 = zeros(91,1);
time = zeros(91,1);
for n=1:0.1:10
tic;
% Various Functions
m = n*10-9;
t(m) = toc;
error1(m)= <first error>;
error2(m)= <second error>;
end
plot([error1, error2, t]);
Since n is not always an integer, m=n*10-9 gives an integer indexing.
  1 件のコメント
Steven Lord
Steven Lord 2016 年 10 月 25 日
Rather than multiply a decimal number to get an index (which does NOT always work) consider iterating over a vector of integer values and transforming the elements of that vector into a decimal.
for n = 1:0.1:5
n2 = 10*n-9;
disp([n, n2])
if n2 ~= round(n2)
fprintf('10*(%g)-9 returns %d%+g.\n', n, round(n2), n2-round(n2));
end
end
Compare with:
for n2 = 1:41
n = (n2+9)/10;
disp([n, n2])
if n2 ~= round(n2)
fprintf('10*(%g)-9 returns %d%+g.\n', n, round(n2), n2-round(n2));
end
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by