Appending new dimensions to existing variable
3 ビュー (過去 30 日間)
古いコメントを表示
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 件)
James Tursa
2012 年 3 月 23 日
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.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!