d77f506d79
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> |
||
---|---|---|
.. | ||
README.md |
New matrices
{:.no_toc}
* TOC {:toc}The goal
Making a new matrix...
Questions to David Rotermund
Using import numpy as np is the standard.
Simple example -- new np.zeros()
Define the size of your new matrix with a tuple, e.g.
M = numpy.zeros((DIM_0, DIM_1, DIM_2, …))
1d
import numpy as np
M = np.zeros((2))
print(M)
Output:
[0. 0.]
2d
import numpy as np
M = np.zeros((2, 3))
print(M)
Output:
[[0. 0. 0.]
[0. 0. 0.]]
3d
import numpy as np
M = np.zeros((2, 3, 4))
print(M)
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.]]]
Simple example -- recycle np.zeros_like()
If you have a matrix with the same size you want then you can use zeros_like. This will also copy other properties like the data type.
as a prototype use
N = numpy.zeros_like(M)
import numpy as np
M = np.zeros((2, 3, 4))
N = np.zeros_like(M)
print(N)
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.]]]
Remember unpacking
{: .topic-optional} This is an optional topic!
import numpy as np
d = (3, 4)
M = np.zeros((2, *d))
print(M)