I'm trying to build a constraint matrix that I'm going to use with linprog
, and I'm struggling to build it efficiently without using for loops. Here is an example of what I'm trying to achieve:
A = zeros(3, 3); % Constraint matrix
i = [1 3]; % row indexes
j = [1 2 ; 1 3]; % column indexes
x = [1 2 ; 3 4]; % values to assign
Expected result after assignment:
A = [1 2 0 ; 0 0 0 ; 3 0 4]
I want to perform the following operations:
A(i(1), j(1,:)) = x(1,:)
A(i(2), j(2,:)) = x(2,:)
For now, I'm using a for loop:
for k=1:length(i)
A(i(k), j(k,:)) = x(k,:);
end
Is there any better way to do this for any i
and j
? I could use a for loop everywhere, but the number of constraint depends on the number of variables, so my code gets filled with for-loops. What's the standard way of defining the constraint matrix to use with linprog
?