# Vectorize this?

1 view (last 30 days)
Edward Umpfenbach on 7 May 2012
yidx = [false(18144,1);true(24,1);false(36882,1);true(64,1);false(6,1);true(198,1)];
temp = find(yidx);
for i=1:1:size(temp,1)
i
for j = 1:1:size(cplex.Model.A,1)
if cplex.Model.A(j,temp(i)) ~= 0
temp_row = cplex.Model.A(j,:);
temp2 = find(temp_row ~= 0);
temp3 = find(temp2 == temp(i));
temp2(temp3) = [];
bin_coefficient = temp_row(1,temp(i));
for k = 1:1:size(temp2,1)
var = [j,temp2(k)];
b = cplex.Model.rhs(j,1) - bin_coefficient;
u = cplex.Model.rhs(j,1);
new_row = sparse(1,size(cplex.Model.A,2));
new_row(1,temp2(k)) = 1;
new_row(1,temp(i)) = (u-b);
cplex.Model.rhs = [cplex.Model.rhs;u];
cplex.Model.lhs = [cplex.Model.lhs;-Inf];
cplex.Model.A = [cplex.Model.A;new_row];
end
end
end
end

Stefan on 7 May 2012
Hi Edward,
which structure has your cplex.Model? Without this information it's more difficult to understand the vectoroizing problem.
greetz

Jan on 7 May 2012
Do you really need a vectorization or do you want to accelerate the function only?
For the later, a pre-allocation is strongly recommended. For some fields this is even identical to the "vectorizatzion":
yidx = [false(18144,1); true(24,1); false(36882,1); true(64,1); false(6,1); true(198,1)];
temp = find(yidx);
A = cplex.Model.A; % Avoid repeated accessing of substructs
for i = 1:size(temp, 1)
for j = 1:size(A, 1)
if A(j,temp(i)) ~= 0
temp_row = A(j,:);
temp2 = find(temp_row ~= 0);
temp2(temp2 == temp(i)) = [];
bin_coefficient = temp_row(1,temp(i));
cplex.Model.lhs = [cplex.Model.lhs, repmat(-Inf, size(temp2,1), 1)];
% b and u do not depend on k loop, so move it outside for speed:
b = cplex.Model.rhs(j,1) - bin_coefficient;
u = cplex.Model.rhs(j,1);
cplex.Model.rhs = [cplex.Model.rhs; repmat(u, size(temp2,1), 1)];
for k = 1:size(temp2, 1)
% Not used, do not shadow the built-in VAR!
% var = [j, temp2(k)];
new_row = sparse(1,size(A,2));
new_row(1, temp2(k)) = 1;
new_row(1, temp(i)) = (u-b);
A = [A; new_row];
end
end
end
end
cplex.Model.A = A;

Stefan on 7 May 2012
I think you can pre-allocate teh varibale new_row also (command 'ones()' )
Variable A is changig it's size in every loop. To accelerate pre-allocate A and use zour index k. Afterwards you can connect the single values with combine or cell2mat