<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Making_a_new_matrix</id>
	<title>Making a new matrix - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Making_a_new_matrix"/>
	<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Making_a_new_matrix&amp;action=history"/>
	<updated>2026-04-20T05:16:30Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Making_a_new_matrix&amp;diff=667&amp;oldid=prev</id>
		<title>Davrot: Created page with &quot;Making a new matrix…  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  Using &#039;&#039;&#039;import numpy as np&#039;&#039;&#039; is the standard.  == Simple example – new [https://numpy.org/doc/stable/reference/generated/numpy.zeros.html np.zeros()] == Define the size of your new matrix with a tuple, e.g.&lt;syntaxhighlight lang=&quot;python&quot;&gt;M = numpy.zeros((DIM_0, DIM_1, DIM_2, …))​&lt;/syntaxhighlight&gt;  === 1d === &lt;syntaxhighlight lang=&quot;python&quot;&gt;import numpy as np  M = np.zeros((2)) prin...&quot;</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Making_a_new_matrix&amp;diff=667&amp;oldid=prev"/>
		<updated>2025-10-23T14:17:41Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Making a new matrix…  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  Using &amp;#039;&amp;#039;&amp;#039;import numpy as np&amp;#039;&amp;#039;&amp;#039; is the standard.  == Simple example – new [https://numpy.org/doc/stable/reference/generated/numpy.zeros.html np.zeros()] == Define the size of your new matrix with a tuple, e.g.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;M = numpy.zeros((DIM_0, DIM_1, DIM_2, …))​&amp;lt;/syntaxhighlight&amp;gt;  === 1d === &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np  M = np.zeros((2)) prin...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Making a new matrix…&lt;br /&gt;
&lt;br /&gt;
Questions to [mailto:davrot@uni-bremen.de David Rotermund]&lt;br /&gt;
&lt;br /&gt;
Using &amp;#039;&amp;#039;&amp;#039;import numpy as np&amp;#039;&amp;#039;&amp;#039; is the standard.&lt;br /&gt;
&lt;br /&gt;
== Simple example – new [https://numpy.org/doc/stable/reference/generated/numpy.zeros.html np.zeros()] ==&lt;br /&gt;
Define the size of your new matrix with a tuple, e.g.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;M = numpy.zeros((DIM_0, DIM_1, DIM_2, …))​&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 1d ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
M = np.zeros((2))&lt;br /&gt;
print(M)&amp;lt;/syntaxhighlight&amp;gt;Output:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;[0. 0.]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 2d ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
M = np.zeros((2, 3))&lt;br /&gt;
print(M)&amp;lt;/syntaxhighlight&amp;gt;Output:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;[[0. 0. 0.]&lt;br /&gt;
 [0. 0. 0.]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 3d ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
M = np.zeros((2, 3, 4))&lt;br /&gt;
print(M)&amp;lt;/syntaxhighlight&amp;gt;Output:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;[[[0. 0. 0. 0.]&lt;br /&gt;
  [0. 0. 0. 0.]&lt;br /&gt;
  [0. 0. 0. 0.]]&lt;br /&gt;
&lt;br /&gt;
 [[0. 0. 0. 0.]&lt;br /&gt;
  [0. 0. 0. 0.]&lt;br /&gt;
  [0. 0. 0. 0.]]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Simple example – recycle [https://numpy.org/doc/stable/reference/generated/numpy.zeros_like.html np.zeros_like()] ==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
as a prototype use&lt;br /&gt;
&lt;br /&gt;
N = numpy.zeros_like(M)&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
M = np.zeros((2, 3, 4))&lt;br /&gt;
&lt;br /&gt;
N = np.zeros_like(M)&lt;br /&gt;
print(N)&amp;lt;/syntaxhighlight&amp;gt;Output:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;[[[0. 0. 0. 0.]&lt;br /&gt;
  [0. 0. 0. 0.]&lt;br /&gt;
  [0. 0. 0. 0.]]&lt;br /&gt;
&lt;br /&gt;
 [[0. 0. 0. 0.]&lt;br /&gt;
  [0. 0. 0. 0.]&lt;br /&gt;
  [0. 0. 0. 0.]]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Remember unpacking ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;This is an optional topic!&amp;#039;&amp;#039;&amp;#039;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
d = (3, 4)&lt;br /&gt;
M = np.zeros((2, *d))&lt;br /&gt;
&lt;br /&gt;
print(M)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== np.empty is not np.zeros ==&lt;br /&gt;
If you are sure that you don’t care about what is inside the matrix in the beginning use&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;M = numpy.empty((DIM_0, DIM_1, DIM_2,...))​&amp;lt;/syntaxhighlight&amp;gt;Empty claims a region in the memory and uses it for a matrix. Zeros goes one step further. It fills the memory with zeros.&lt;br /&gt;
&lt;br /&gt;
Thus random junk​ (i.e. data that was stored prior at that memory position) with be the content of a matrix if you use empty. However, np.empty() is faster than np.zeros().&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;​import numpy as np&lt;br /&gt;
M = np.empty((10, 4))&lt;br /&gt;
print(M)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;[[1.66706425e-316 0.00000000e+000 6.89933729e-310 6.89933730e-310]&lt;br /&gt;
 [6.89933729e-310 6.89933730e-310 6.89933729e-310 6.89933730e-310]&lt;br /&gt;
 [6.89933730e-310 6.89933730e-310 6.89933729e-310 6.89933729e-310]&lt;br /&gt;
 [6.89933730e-310 6.89933729e-310 6.89933730e-310 6.89933729e-310]&lt;br /&gt;
 [6.89933730e-310 4.30513389e-317 4.30321296e-317 6.89933825e-310]&lt;br /&gt;
 [4.30389280e-317 6.89933822e-310 4.30366750e-317 6.89933822e-310]&lt;br /&gt;
 [4.30311810e-317 4.30480583e-317 4.30462401e-317 4.30336316e-317]&lt;br /&gt;
 [6.89933822e-310 4.30386513e-317 4.30358055e-317 4.30571886e-317]&lt;br /&gt;
 [4.30568724e-317 4.30659237e-317 6.89933822e-310 6.89933822e-310]&lt;br /&gt;
 [6.89933822e-310 6.89933822e-310 4.30289676e-317 6.89920336e-310]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://numpy.org/doc/stable/reference/routines.array-creation.html#from-shape-or-value From shape or value] ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|[https://numpy.org/doc/stable/reference/generated/numpy.empty.html#numpy.empty empty](shape[, dtype, order, like])&lt;br /&gt;
|Return a new array of given shape and type, without initializing entries.&lt;br /&gt;
|-&lt;br /&gt;
|[https://numpy.org/doc/stable/reference/generated/numpy.empty_like.html#numpy.empty_like empty_like](prototype[, dtype, order, subok, …])&lt;br /&gt;
|Return a new array with the same shape and type as a given array.&lt;br /&gt;
|-&lt;br /&gt;
|[https://numpy.org/doc/stable/reference/generated/numpy.eye.html#numpy.eye eye](N[, M, k, dtype, order, like])&lt;br /&gt;
|Return a 2-D array with ones on the diagonal and zeros elsewhere.&lt;br /&gt;
|-&lt;br /&gt;
|[https://numpy.org/doc/stable/reference/generated/numpy.identity.html#numpy.identity identity](n[, dtype, like])&lt;br /&gt;
|Return the identity array.&lt;br /&gt;
|-&lt;br /&gt;
|[https://numpy.org/doc/stable/reference/generated/numpy.ones.html#numpy.ones ones](shape[, dtype, order, like])&lt;br /&gt;
|Return a new array of given shape and type, filled with ones.&lt;br /&gt;
|-&lt;br /&gt;
|[https://numpy.org/doc/stable/reference/generated/numpy.ones_like.html#numpy.ones_like ones_like](a[, dtype, order, subok, shape])&lt;br /&gt;
|Return an array of ones with the same shape and type as a given array.&lt;br /&gt;
|-&lt;br /&gt;
|[https://numpy.org/doc/stable/reference/generated/numpy.zeros.html#numpy.zeros zeros](shape[, dtype, order, like])&lt;br /&gt;
|Return a new array of given shape and type, filled with zeros.&lt;br /&gt;
|-&lt;br /&gt;
|[https://numpy.org/doc/stable/reference/generated/numpy.zeros_like.html#numpy.zeros_like zeros_like](a[, dtype, order, subok, shape])&lt;br /&gt;
|Return an array of zeros with the same shape and type as a given array.&lt;br /&gt;
|-&lt;br /&gt;
|[https://numpy.org/doc/stable/reference/generated/numpy.full.html#numpy.full full](shape, fill_value[, dtype, order, like])&lt;br /&gt;
|Return a new array of given shape and type, filled with fill_value.&lt;br /&gt;
|-&lt;br /&gt;
|[https://numpy.org/doc/stable/reference/generated/numpy.full_like.html#numpy.full_like full_like](a, fill_value[, dtype, order, …])&lt;br /&gt;
|Return a full array with the same shape and type as a given array.&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
</feed>