First time here? Check out the FAQ!

Ask Your Question
1

Replace rows an columns by zeros in a numpy array

asked 11 years ago

mresimulator gravatar image

updated 10 years ago

FrédéricC gravatar image

Hi expers.

¿How can I replace rows an columns by zeros (or other values) in a numpy array?

Waiting for your answers.

Thanks a lot.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 11 years ago

slelievre gravatar image

updated 11 years ago

sage: import numpy as np

To initialize the array, input python floats (with trailing r): numpy converts them to numpy floats while it would treat Sage reals as Python objects.

sage: a = np.array([[1.r, 2.r, 3.r], [4.r, 5.r, -2.r],[6.r, 7.r, 9.r]])
sage: print a
[[ 1.  2.  3.]
 [ 4.  5. -2.]
 [ 6.  7.  9.]]

Replace line 1 by ones.

sage: a[1] = np.ones(3)
sage: print a
[[ 1.  2.  3.]
 [ 1.  1.  1.]
 [ 6.  7.  9.]]

Replace column 1 by zeros.

sage: a[:,1] = np.zeros(3)
sage: print a
[[ 1.  0.  3.]
 [ 1.  0.  1.]
 [ 6.  0.  9.]]

Replace line 0 by chosen values. Trailing r no longer needed, numpy converts Sage reals to the type of other elements of the array.

sage: a[0] = [3.,3.,6.]
sage: print a
[[ 3.  3.  6.]
 [ 1.  0.  1.]
 [ 6.  0.  9.]]
Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 11 years ago

Seen: 20,798 times

Last updated: Jul 29 '13