Ask Your Question
1

Replace rows an columns by zeros in a numpy array

asked 2013-07-29 12:13:08 +0100

mresimulator gravatar image

updated 2015-01-14 10:18:00 +0100

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.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2013-07-29 21:45:42 +0100

slelievre gravatar image

updated 2013-07-29 21:53:45 +0100

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.]]
edit flag offensive delete link more

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: 2013-07-29 12:13:08 +0100

Seen: 20,367 times

Last updated: Jul 29 '13