Appending new dimensions to existing variable
조회 수: 7 (최근 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에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!