Python Matrix Question: How do I make the right top, left top, right bottom, left bottom only 1 and the rest zero?

Asked 2 years ago, Updated 2 years ago, 67 views

How do you make the top right, top left, bottom right, bottom left, and bottom left only 1 and the rest zero?

[[1 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 1]]

[[ 0 0],
[0 0 0],
[1 0 0]]

python matrix

2022-09-20 17:28

1 Answers

>>> import numpy as np
>>> a = np.zeros((3, 3))
>>> a
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
>>> a[0,0] = 1
>>> a
array([[1., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
>>> a[-1, 0] = 1
>>> a
array([[1., 0., 0.],
       [0., 0., 0.],
       [1., 0., 0.]])
>>> a[0, -1] = 1
>>> a
array([[1., 0., 1.],
       [0., 0., 0.],
       [1., 0., 0.]])
>>> a[-1, -1] = 1
>>> a
array([[1., 0., 1.],
       [0., 0., 0.],
       [1., 0., 1.]])


2022-09-20 17:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.