Effacer les filtres
Effacer les filtres

Appending new dimensions to existing variable

1 vue (au cours des 30 derniers jours)
Igor de Britto
Igor de Britto le 23 Mar 2012
Hi, people,
I'm running a model with 6 input variables and trying to get the results for each case. For instance, if variables were "a", "b", "c", "d", "e", "f" and "g", each of them assuming values from X to Y, there would be 6^12 results I'd like to store on their own.
I do have to plot them controlling wich of them are changing.
Therefore, my idea is to make a structured data like this:
for a = min:max
for b = min:max
for c = min:max
for d = min:max
for e = min:max
for f = min:max
for g = min:max
result{a,b,c,d,e,f,g} = myfunc(a,b,c,d,e,f,g)
end
end
end
end
end
end
It works fine for small computations, say changing only two of the variables. However, when dealing with big computations (like changing all of the variables through of all of the possible values, in order to get the full spectrum of the results), it takes way too long because of the increasing size matrices that are copied when appending.
My question is: is it possible to break the computation into smaller loops, creating smaller matrices, and, after having all of the matrices produced, append them into the originally thought multidimensional results (result{a,b,c,d,e,f,g})? If so, how can it be done?
Cheers!
  1 commentaire
Igor de Britto
Igor de Britto le 23 Mar 2012
Forgot to mention, but the stored data is a n-by-m matrix of integers!

Connectez-vous pour commenter.

Réponses (1)

James Tursa
James Tursa le 23 Mar 2012
Two options to consider:
1) Preallocate your cell array so that it is not changing size throughout your looping. Put this statement prior to your loops:
result = cell(amax,bmax,cmax,dmax,emax,fmax,gmax); % pre-allocate
2) Change result to be a multi-dimensional matrix and store everything in one nD array. E.g.,
result = zeros(n,m,amax,bmax,cmax,dmax,emax,fmax,gmax); % pre-allocate
:
result(:,:,a,b,c,d,e,f,g) = myfunc(a,b,c,d,e,f,g);
Which is better will depend on how you use result in your downstream code.
  1 commentaire
Igor de Britto
Igor de Britto le 23 Mar 2012
If I understood the idea, the first option is to make the whole matrix pre-exist and, as computation goes, substitute the empty cell by the result. So, in order to break it into different computations, it would require save/load or some other mean of detecting if that has already been filled, like isempty, right? Also, the preallocation would happen only once, not at every time the script is run, I guess?
As for the second, is it quicker to do it like that? I mean, it looks almost the same the structured data I suggested. Does the fact that there's only one matrix to which is appended one element at a time makes it faster than to create another matrix?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by