MATLAB Answers


Elements in a vector

Asked by Giuseppe Pintori on 20 Sep 2019
Latest activity Commented on by Stephen Cobeldick on 21 Sep 2019
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.


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.
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];
k = 0;
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?
Stephen Cobeldick on 21 Sep 2019
"is there a way to create a folder named 'rho_ (value assumed by rho in the simulation)' for every simulation?"
Of course:
doc sprintf
doc fullfile
doc mkdir

Sign in to comment.

0 Answers