<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Indexing vectors and arrays in Python</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2013/02/27/Indexing-vectors-and-arrays-in-Python</link>
      <pubDate>Wed, 27 Feb 2013 14:50:40 EST</pubDate>
      <category><![CDATA[basic]]></category>
      <guid isPermaLink="false">lYjCRuLMYclGc-u4wSihJmTO9m0=</guid>
      <description>Indexing vectors and arrays in Python</description>
      <content:encoded><![CDATA[


&lt;p&gt;
&lt;a href="http://matlab.cheme.cmu.edu/2011/08/24/indexing-vectors-and-arrays-in-matlab/" &gt;Matlab post&lt;/a&gt;
There are times where you have a lot of data in a vector or array and you want to extract a portion of the data for some analysis. For example, maybe you want to plot column 1 vs column 2, or you want the integral of data between x = 4 and x = 6, but your vector covers 0 &amp;lt; x &amp;lt; 10. Indexing is the way to do these things.
&lt;/p&gt;

&lt;p&gt;
A key point to remember is that in python array/vector indices start at 0. Unlike Matlab, which uses parentheses to index a array, we use brackets in python.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; np

x = np.linspace(-np.pi, np.pi, 10)
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; x

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; x[0]  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;first element&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; x[2]  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;third element&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; x[-1] &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;last element&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; x[-2] &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;second to last element&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
&amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; [-3.14159265 -2.44346095 -1.74532925 -1.04719755 -0.34906585  0.34906585
  1.04719755  1.74532925  2.44346095  3.14159265]
&amp;gt;&amp;gt;&amp;gt; -3.14159265359
-1.74532925199
3.14159265359
2.44346095279
&lt;/pre&gt;

&lt;p&gt;
We can select a range of elements too. The syntax a:b extracts the a^{th} to (b-1)^{th} elements. The syntax a:b:n starts at a, skips nelements up to the index b.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; x[1:4]     &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;second to fourth element. Element 5 is not included&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; x[0:-1:2]  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;every other element&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; x[:]       &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;print the whole vector&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; x[-1:0:-1] &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;reverse the vector!&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[-2.44346095 -1.74532925 -1.04719755]
[-3.14159265 -1.74532925 -0.34906585  1.04719755  2.44346095]
[-3.14159265 -2.44346095 -1.74532925 -1.04719755 -0.34906585  0.34906585
  1.04719755  1.74532925  2.44346095  3.14159265]
[ 3.14159265  2.44346095  1.74532925  1.04719755  0.34906585 -0.34906585
 -1.04719755 -1.74532925 -2.44346095]
&lt;/pre&gt;

&lt;p&gt;
Suppose we want the part of the vector where x &amp;gt; 2. We could do that by inspection, but there is a better way. We can create a mask of boolean (0 or 1) values that specify whether x &amp;gt; 2 or not, and then use the mask as an index.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; x[x &amp;gt; 2]
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[ 2.44346095  3.14159265]
&lt;/pre&gt;

&lt;p&gt;
You can use this to analyze subsections of data, for example to integrate the function y = sin(x) where x &amp;gt; 2.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;y = np.sin(x)

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; np.trapz( x[x &amp;gt; 2], y[x &amp;gt; 2])
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
&amp;gt;&amp;gt;&amp;gt; -1.79500162881
&lt;/pre&gt;

&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; 2d arrays&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
In 2d arrays, we use  row, column notation. We use a : to indicate all rows or all columns.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;a = np.array([[1, 2, 3], 
              [4, 5, 6], 
              [7, 8, 9]])

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; a[0, 0]
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; a[-1, -1]

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; a[0, :] &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;row one&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; a[:, 0] &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;column one&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; a[:]
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
... &amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; 1
9
&amp;gt;&amp;gt;&amp;gt; [1 2 3]
[1 4 7]
[[1 2 3]
 [4 5 6]
 [7 8 9]]
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;&lt;span class="section-number-2"&gt;2&lt;/span&gt; Using indexing to assign values to rows and columns&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;b = np.zeros((3, 3))
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; b

b[:, 0] = [1, 2, 3] &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;set column 0&lt;/span&gt;
b[2, 2] = 12        &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;set a single element&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; b

b[2] = 6  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;sets everything in row 2 to 6!&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; b
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]
&amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; [[  1.   0.   0.]
 [  2.   0.   0.]
 [  3.   0.  12.]]
&amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; [[ 1.  0.  0.]
 [ 2.  0.  0.]
 [ 6.  6.  6.]]
&lt;/pre&gt;

&lt;p&gt;
Python does not have the linear assignment method like Matlab does. You can achieve something like that as follows. We flatten the array to 1D, do the linear assignment, and reshape the result back to the 2D array.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;c = b.flatten()
c[2] = 34
b[:] = c.reshape(b.shape)
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; b
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
&amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; [[  1.   0.  34.]
 [  2.   0.   0.]
 [  6.   6.   6.]]
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;&lt;span class="section-number-2"&gt;3&lt;/span&gt; 3D arrays&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;
&lt;p&gt;
The 3d array is like book of 2D matrices. Each page has a 2D matrix on it. think about the indexing like this: (row, column, page)
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;M = np.random.uniform(size=(3,3,3))  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;a 3x3x3 array&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; M
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[[[ 0.78557795  0.36454381  0.96090072]
  [ 0.76133373  0.03250485  0.08517174]
  [ 0.96007909  0.08654002  0.29693648]]

 [[ 0.58270738  0.60656083  0.47703339]
  [ 0.62551477  0.62244626  0.11030327]
  [ 0.2048839   0.83081982  0.83660668]]

 [[ 0.12489176  0.20783996  0.38481792]
  [ 0.05234762  0.03989146  0.09731516]
  [ 0.67427208  0.51793637  0.89016255]]]
&lt;/pre&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; M[:, :, 0]  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;2d array on page 0&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; M[:, 0, 0]  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;column 0 on page 0&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; M[1, :, 2]  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;row 1 on page 2&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[[ 0.78557795  0.76133373  0.96007909]
 [ 0.58270738  0.62551477  0.2048839 ]
 [ 0.12489176  0.05234762  0.67427208]]
[ 0.78557795  0.58270738  0.12489176]
[ 0.47703339  0.11030327  0.83660668]
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-4" class="outline-2"&gt;
&lt;h2 id="sec-4"&gt;&lt;span class="section-number-2"&gt;4&lt;/span&gt; Summary&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;
&lt;p&gt;
The most common place to use indexing is probably when a function returns an array with the independent variable in column 1 and solution in column 2, and you want to plot the solution. Second is when you want to analyze one part of the solution. There are also applications in numerical methods, for example in assigning values to the elements of a matrix or vector.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2013 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2013/02/27/Indexing-vectors-and-arrays-in-Python.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
