Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

Setting diagonal of a matrix to zero

$
0
0
m = [[[math.inf] * 10] for i in range(10)]for i in range(len(m)):    for j in range(len(m)):        if(i == j):            m[i][i] = 0

I try to insert zero And I used the debugger to watch the process go. Instead of changing it to

[[[0,inf,inf,inf,inf,inf,inf,inf,inf,inf],[......],[.......]]]

It changes it to

[[0], [[inf,inf,inf,inf....],[inf,inf,....]]

Which then causes an index out of bound error because it just reduced a 10x10 matrix to the top row being zero,as opposed to inf,inf,inf,inf,inf,inf,inf,inf,inf,inf.

Where my goal is to set a 10x10 matrix's diagonal to all zeros.

I tried to use:

m = [[[math.inf] * 10] for i in range(10)]

Viewing all articles
Browse latest Browse all 13891

Trending Articles