5
$\begingroup$

I have compiled lists of non-diagonal and diagonal entries for a matrix. I'm trying to add elements from the diagonal-entry lists into certain entries of the non-diagonal lists. Is there a feasible way I can add these lists without retyping everything up in a new list? Or should I just try another approach?

I've listed examples of my lists in a much shortened version for simplicity; in reality, I'm trying to create a matrix of size 30x30. I've tried messing around with the Part[] function, but haven't been able to get it to work the way I desire.

non-diags = {{0,0,-1},{0,0,-1},{-1,-1,0}}
diags = {5,4,6}

My desired list is:

matrixlist = {{5,0,-1},{0,4,-1},{-1,-1,6}}

Thanks for any help with this!

$\endgroup$
0

1 Answer 1

8
$\begingroup$
nondiags = {{0, 0, -1}, {0, 0, -1}, {-1, -1, 0}};
diags = {5, 4, 6};
DiagonalMatrix[diags] + nondiags

$\endgroup$

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.