How can I improve Matlab perfomance for this code?
Show older comments
Adn and custo are same size vectors, with a length of 10252 elements (length(Adn)=10252). Every row value in custo corresponds to the cost of the same row value in Adn . I'm trying to get the sum of all the possible combinations for the values in Adn and analyze the cost of that combination. From the Adn combinations, I am just interested in the ones that sum to a specific interval (which is lower than sum(A) and greater than sum(A)*0.9). A is previously written in the code, so it doesn't matter now. Then I want to store the combinations and their equivalent costs ( custoAll ) in two new vectors.
nA=numel(Adn);
for k1=1:nA
B=combntns(Adn,k1);
C=sum(B,2);
E=combntns(custo,k1);
F=sum(E,2);
D=find(((C<(sum(A)))&(C>(sum(A)*0.9))));
if ~isempty(D)
combinations=[combinations; B(D,:)];
custoAll=[custoAll; F(D)];
end
end
This code is taking very long though. I know I should preallocate the variables combinations and custoAll otherwise they will change size in every iteration and it'll take longer to run, but I haven't been able to find a good way to do it, since Adn is already so huge. Does anybody have an insight on this?
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!