Writing result of script (ran in a loop) into vector

1 次查看(过去 30 天)
Hey,
I want to execute a script (main) in a loop which gives me a different result (solvertime) every time I run it and write this result into a vector with the size of the Number of runs.
NumOfRuns = 3;
results=zeros(1,NumOfRuns);
for i=1:NumOfRuns
main;
results(i)=solvertime;
end
For some reason it gives me this vector. Help is much appreciated.
  4 个评论
Rik
Rik 2020-10-12
Regarding your flag ("I found out that the answer to this question was that "i" was somewhere in the script. so no one in this forum could know the answer. delete this please"):
We tend not to delete questions (especially those that have received valid attempts at an answer) unless there are very good reasons for doing so. Often the underlying reason is that the content should never have been made public in the first place. Because it is hard to say if something is usefull to some random passer-by in the future, we tend to want to keep content as-is. I don't see any such reason for deletion in your post, so I'm going to remove the flag. If you disagree, feel free to explain in a comment why you believe this question should be deleted. Just in case you were planning to delete the question content: I can easily restore it from this Wayback Machine capture, so you don't have to bother.
(Even though I do have some editorial priviliges through the reputation system, I am not afiliated with Mathworks and don't control anything on this website. Feel free to contact support if you want Mathworks to make a decision about this post.)

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2020-10-9
Something does not make much sense: given NumOfRuns = 3, why does results have 26 columns?
I suspect that the script also contains a variable named results, in which case there are two main solutions:
  1. rename one/both of those variables so that they have different names
  2. turn your script into a function (preferred approach).
  2 个评论
Jonas
Jonas 2020-10-9
hey,
  1. renaming the variables in this script (as shown above, not main) have the same results
I guess it has something to do with script (main) because it is quite complex and it interferres with that loop function in some way
Stephen23
Stephen23 2020-10-9
编辑:Stephen23 2020-10-9
This is why experienced MATLAB users avoid scripts for any real work. Scripts are fun and easy to play with, but if you want reliable, repeatable, reuable code then functions/classes are the only way to go.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by