You typically do not want to fiddle with out dolfin decides to order the rows in the matrix. A better alternative would be to locate the rows/cols which need to be changed to account for your custom procedure. See for instance How to find the coordinate where a vector function is max? - #2 by dokken , and several other posts in this forum is you search with the name of that function.