You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Elements in a vector
9 views (last 30 days)
Show older comments
Hi guys, I need your help.
I have a vector like the following:
rho = [0.8 : -0.01 : -0.8];
The problem is: for every simulation matlab should take in consideration the value of the vector; basically, for the first simulation should take rho = 0.8, for the second rho = 0.79 and so on.
I don't know what function is able to do something like that.
Moreover, I would like to create at the end of each simulation a folder called as the parameter and the value taken into consideration in that simulation (simulation 1, rho= 0.8, foldername rho_0.8); I've tried with the nextname function but I'm not able to use it in this situation.
Thanks in advance for any help.
14 Comments
Walter Roberson
on 20 Sep 2019
Giuseppe Pintori
on 20 Sep 2019
Is there a way to avoid using a for loop? Furthermore, I do not understand how to apply your suggestion to my problem
Giuseppe Pintori
on 20 Sep 2019
Basically, my variable of interest (rho) is just one of many variables that I must use to evaluate my model; I would prefer not to burden the computation.
I don't know if I explained myself well.
Giuseppe Pintori
on 20 Sep 2019
Depending on the values assumed by the parameters from time to time, the simulation takes a lot of time. As a result, I would prefer the code to be as "lean" as possible
Stephen23
on 20 Sep 2019
Edited: Stephen23
on 20 Sep 2019
"Depending on the values assumed by the parameters from time to time, the simulation takes a lot of time.
And yet you omitted to mention this important information from your question.
The first and most important step is to make your code as efficient as possible.
Some users want to ignore this important step and jump right in to much more exciting things like parallel computation, rather than learning the ways to write efficient code. It is well worth locating the bottle-necks and figuring out how to improve your algorithm:
After ensuring that your simulation is reasonably efficient you could investigate things like parfor:
But really the answer depends on the nature of your simulation, the nature of your input/output data, what computer HW you have available, etc.,
Giuseppe Pintori
on 20 Sep 2019
The code was developed to compute a theoretical model and, however unstable, it meets my needs.
Inside it you can find parfor that are needed to get to my output but, in this phase, I have to do comparative static and the rho is one of them.
Going back to my initial request, is it possible to do what I requested without using additional for? Otherwise I can try with an additional one
Giuseppe Pintori
on 20 Sep 2019
So, to avoid using a for, the only solution is to reduce the value of rho by hand for each simulation?
Stephen23
on 20 Sep 2019
Edited: Stephen23
on 20 Sep 2019
"Going back to my initial request, is it possible to do what I requested without using additional for? "
Possibly. As I wrote earlier, the answer depends on the nature of your simulation, the nature of your input/output data, what computer HW you have available, etc.
"So, to avoid using a for, the only solution is to reduce the value of rho by hand for each simulation?"
I have no idea what it means to "reduce the value of rho by hand for each simulation", or how this relates to for loops or to the efficiency of your code.
Giuseppe Pintori
on 20 Sep 2019
It means not creating a vector; consequently I would have my variable rho which is reduced by 0.1 every simulation
Walter Roberson
on 20 Sep 2019
There is arrayfun(), which is just a hidden for loop (though these days it is built-in rather than a .m file)
The more general technique is to vectorize your code to be able to process multiple rho values at the same time. In some cases you can get a fair bit of performance improvement through vectorizing; in other cases, vectorizing can slow your code down.
I think you will find that adding a for loop over rho values will add very little cost compared to the cost of your simulation. for loops used to have higher costs than now, but performance was improved for them several years ago.
Giuseppe Pintori
on 21 Sep 2019
Guys, at the moment I'm using this code in order to change the value of my parameter in every simulation:
rho = [0.8 : -0.01 : -0.8];
try
isempty(k)
catch
k = 0;
end
k = k+1;
var = rho(1,k);
Now, is there a way to create a folder named 'rho_ (value assumed by rho in the simulation)' for every simulation?
Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)