I don’t quite understand the desired output, as it seems you are not setting the values in C to A[i] if B[i]!=-1.
E.g. the second row is given as [x_1, x_1, x_4] and the last one as [x_3, x_3, x_3]. Could you explain a bit more how these results are created?