2 Loops of same size, 1 goes very quick but one is very slow.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Kernel7364
el 2 de Mayo de 2019
Comentada: Kernel7364
el 8 de Mayo de 2019
Hello,
I have 2 loops with 4 index each. Every loop is of this form
X1 =zeros (10,11);
s= zeros(1,10^2*11^2);
for i = 1:10
for j = 1:10
for k = 1:11
for l = 1:11
counter = counter + 1
s(counter) = 2;
X1(i,k) = X1(i,k) - 1;
X1(j,l) = X1(j,l) - 1;
X = reshape(X1',1,[]);
T1 = {s,X} ;
V1 = horzcat(T1{:}) ;
A(counter,:) = V1(:);
X1 =zeros(10,11);
s= zeros(1,10^2*11^2);
end
end
end
end
The first loop does what it should do very quickly. And the second one is exactly of the same form but this runs very slowly.
I watch the variable "counter" to see how fast it runs and the first loop is finished after a few seconds, but the second one needs already 2 minutes for the first 500 cycles.
Why is it so slow and what can I do to make it faster ?
Best regards !
4 comentarios
Guillaume
el 2 de Mayo de 2019
Are you sure that counter is set to 0 before you start any of these loops? If not, you'll be growing s slowly in the loops to sizes far greater than 12100. I note that there are no
counter = 0;
in any of the code you've posted.
But your second advice is not what I want
The whole thing can be constructed without any loop whatsoever, so please explain exactly what your constraints are. In particular, at the moment your inner loops iterate over columns and the outer loops iterate over rows, which is less efficient than iterating over the rows in the inner loops. So is the order of iteration critical?
Respuesta aceptada
Guillaume
el 2 de Mayo de 2019
Editada: Guillaume
el 2 de Mayo de 2019
Here is a simple way to create your A (the one with +1 and +2)
[v1, v2] = ndgrid(num2cell(eye(110), 2)); %110 is 10*11
vsum = cellfun(@plus, v1, v2, 'UniformOutput', false);
vsum = cell2mat(vsum(:));
Apos = [2*eye(size(vsum, 1)), vsum];
Your A with -1 and -2 is simply:
Aneg = [2*eye(size(vsum, 1)), -vsum];
Note that the order of the rows is going to be different from what your loops generate but I assume it doesn't matter.
13 comentarios
Guillaume
el 7 de Mayo de 2019
The Cplex solver needed a long time and if I do it for a bigger one it did not solve it after 2 hours
I'm afraid this is not something I can help with. It's completely outside my domain of expertise, I don't even know what CPlex is. However, I wouldn't be surprised if it took a long time to find a solution to whatever it is you're trying to solve. You have 12100 unknown!
The class Cplex has no Constant property or Static method named 'solve'.
Sounds like solve is a normal method of the class, not a static method. If so, you need to call it on an instance of the class, not on the class itself. Something like:
myobj = CPlex(???); %construct an instance of the CPlex class. Replace the ??? by the required inputs if any.
myobj.solve; %call solve on the instance.
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!