sdgaw erzswer
sdgaw erzswer

Reputation: 2382

Manipulating numpy matrices..replacing a value

I've recently discovered numpy and its usefulness when it comes to matrices. There is one thing that still bothers me tho. How do I replace a specific value in a matrix with a desired one. I've tried>

matrix1 = np.zeros([10,10])
replacewith = 1
atposition = np.array(5,5)
matrix1[atposition] = replacewith

So what I'm trying to do is replace the middle 0 with 1. Thank you!

Upvotes: 0

Views: 83

Answers (1)

nwk
nwk

Reputation: 4050

import numpy as np
matrix1 = np.zeros([10,10])
replacewith = 1
atposition = (5,5)
matrix1[atposition] = replacewith
print matrix1

Output:

[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  1.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

Upvotes: 5

Related Questions