z = Array[Subscript[y, ##] &, {3, 2}]
f = {2, 5, 1}
Total[Flatten[ z*f]]
Minimize[{Total[Flatten[ z*f]], 
  Total[z] == 10 && NonNegative[Min[z]]}, z, Integers]

The above code works for one-dimensional matrix z, But when z is two-dimensional, this error occurs:

Minimize::ivar: {Subscript[y, 1,1],Subscript[y, 1,2]} is not a valid variable. >>

Anyone have any idea why this happens?



1 Answer 1


One has to provide the variables as a flat list. Therefore you just have to replace z with Flatten[z] in your Minimize:

Minimize[{Total[Flatten[z*f]], Total[z] == 10 && NonNegative[Min[z]]}, Flatten@z, Integers]



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.