<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://gisaxs.com/index.php?action=history&amp;feed=atom&amp;title=Python%3AIndexing</id>
	<title>Python:Indexing - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://gisaxs.com/index.php?action=history&amp;feed=atom&amp;title=Python%3AIndexing"/>
	<link rel="alternate" type="text/html" href="http://gisaxs.com/index.php?title=Python:Indexing&amp;action=history"/>
	<updated>2026-04-08T22:35:56Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.7</generator>
	<entry>
		<id>http://gisaxs.com/index.php?title=Python:Indexing&amp;diff=5788&amp;oldid=prev</id>
		<title>KevinYager: /* General */</title>
		<link rel="alternate" type="text/html" href="http://gisaxs.com/index.php?title=Python:Indexing&amp;diff=5788&amp;oldid=prev"/>
		<updated>2017-07-19T17:49:08Z</updated>

		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;General&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 17:49, 19 July 2017&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l30&quot; &gt;Line 30:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 30:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;ycur = y[idx]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;ycur = y[idx]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print &amp;#039;x search: &amp;#039;, x_search, idx, xcur, ycur&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print &amp;#039;x search: &amp;#039;, x_search, idx, xcur, ycur&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;# Find closest to specific value&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;idx = (np.abs(x-x_search)).argmin()&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>KevinYager</name></author>
		
	</entry>
	<entry>
		<id>http://gisaxs.com/index.php?title=Python:Indexing&amp;diff=4503&amp;oldid=prev</id>
		<title>KevinYager at 13:47, 29 October 2014</title>
		<link rel="alternate" type="text/html" href="http://gisaxs.com/index.php?title=Python:Indexing&amp;diff=4503&amp;oldid=prev"/>
		<updated>2014-10-29T13:47:14Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 13:47, 29 October 2014&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot; &gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This page just collects some notes/hints about how indexing is done in Python and numpy.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This page just collects some notes/hints about how indexing is done in &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;[[&lt;/ins&gt;Python&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;]] &lt;/ins&gt;and numpy.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==General==&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==General==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>KevinYager</name></author>
		
	</entry>
	<entry>
		<id>http://gisaxs.com/index.php?title=Python:Indexing&amp;diff=4461&amp;oldid=prev</id>
		<title>KevinYager at 20:06, 15 October 2014</title>
		<link rel="alternate" type="text/html" href="http://gisaxs.com/index.php?title=Python:Indexing&amp;diff=4461&amp;oldid=prev"/>
		<updated>2014-10-15T20:06:22Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 20:06, 15 October 2014&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot; &gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;This page just collects some notes/hints about how indexing is done in Python and numpy.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==General==&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==General==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;source lang=&amp;quot;python&amp;quot; line &amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;source lang=&amp;quot;python&amp;quot; line &amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>KevinYager</name></author>
		
	</entry>
	<entry>
		<id>http://gisaxs.com/index.php?title=Python:Indexing&amp;diff=4460&amp;oldid=prev</id>
		<title>KevinYager: Created page with &quot;==General== &lt;source lang=&quot;python&quot; line &gt; #!/usr/bin/python # -*- coding: utf-8 -*-  import numpy as np  # Test data x = np.linspace( 10.0, 20.0, 20 ) def gaussian( x, xc=0.0, ...&quot;</title>
		<link rel="alternate" type="text/html" href="http://gisaxs.com/index.php?title=Python:Indexing&amp;diff=4460&amp;oldid=prev"/>
		<updated>2014-10-15T19:58:54Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==General== &amp;lt;source lang=&amp;quot;python&amp;quot; line &amp;gt; #!/usr/bin/python # -*- coding: utf-8 -*-  import numpy as np  # Test data x = np.linspace( 10.0, 20.0, 20 ) def gaussian( x, xc=0.0, ...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==General==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot; line &amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# -*- coding: utf-8 -*-&lt;br /&gt;
&lt;br /&gt;
import numpy as np&lt;br /&gt;
&lt;br /&gt;
# Test data&lt;br /&gt;
x = np.linspace( 10.0, 20.0, 20 )&lt;br /&gt;
def gaussian( x, xc=0.0, sigma=1.0 ):&lt;br /&gt;
    y = np.exp( -( np.square(x-xc) )/( (2*sigma)**2 ) )&lt;br /&gt;
    return y&lt;br /&gt;
y = gaussian( x, xc=15.0, sigma=2.0 )&lt;br /&gt;
for i, (xcur, ycur) in enumerate( zip(x,y) ):&lt;br /&gt;
    print i, xcur, ycur&lt;br /&gt;
print &amp;#039;\n\n&amp;#039;&lt;br /&gt;
&lt;br /&gt;
# The maximum value&lt;br /&gt;
idx_max = np.argmax( y )&lt;br /&gt;
x_max = x[idx_max]&lt;br /&gt;
y_max = y[idx_max]&lt;br /&gt;
print &amp;#039;max: &amp;#039;, idx_max, x_max, y_max&lt;br /&gt;
&lt;br /&gt;
# Find a specific value along x&lt;br /&gt;
x_search = 15.0&lt;br /&gt;
idx = np.where( x&amp;gt;x_search )[0][0]&lt;br /&gt;
xcur = x[idx]&lt;br /&gt;
ycur = y[idx]&lt;br /&gt;
print &amp;#039;x search: &amp;#039;, x_search, idx, xcur, ycur&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Sort an x and y series&lt;br /&gt;
x = [ 4.0, 3.0, 1.0, 5.0, 6.0, 8.0, 2.0, 7.0, 9.0, 10.0 ]&lt;br /&gt;
y = [ 40.0, 30.0, 10.0, 50.0, 60.0, 80.0, 20.0, 70.0, 90.0, 100.0 ]&lt;br /&gt;
&lt;br /&gt;
x = np.asarray(x)&lt;br /&gt;
y = np.asarray(y)&lt;br /&gt;
indices = np.argsort( x )&lt;br /&gt;
print indices&lt;br /&gt;
x_sorted = x[indices]&lt;br /&gt;
y_sorted = y[indices]&lt;br /&gt;
print x_sorted&lt;br /&gt;
print y_sorted&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Remove a particular element from x and y==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot; line &amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# -*- coding: utf-8 -*-&lt;br /&gt;
&lt;br /&gt;
import numpy as np&lt;br /&gt;
&lt;br /&gt;
# Remove NaNs&lt;br /&gt;
print &amp;#039;\n\n&amp;#039;&lt;br /&gt;
data = np.array([[1, 2, 3, 4],&lt;br /&gt;
           [10, 40, np.nan, 160]])&lt;br /&gt;
data = np.transpose(data)&lt;br /&gt;
x = data[:,0]&lt;br /&gt;
y = data[:,1]&lt;br /&gt;
&lt;br /&gt;
idx = np.isnan( y )&lt;br /&gt;
xcur = x[~idx]&lt;br /&gt;
ycur = y[~idx]&lt;br /&gt;
print xcur, ycur&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
See also [http://docs.scipy.org/doc/numpy/reference/generated/numpy.nan_to_num.html numpy.nan_to_num].&lt;br /&gt;
&lt;br /&gt;
==Replace items according to a rule==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot; line &amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
# -*- coding: utf-8 -*-&lt;br /&gt;
&lt;br /&gt;
import numpy as np&lt;br /&gt;
&lt;br /&gt;
# Replace zero elements&lt;br /&gt;
print &amp;#039;\n\n&amp;#039;&lt;br /&gt;
x = np.array( [-1,0,0.01,0.1,1.0,10.0] )&lt;br /&gt;
print x&lt;br /&gt;
&lt;br /&gt;
idx = np.nonzero(x==0)&lt;br /&gt;
x[idx] = 1e-9&lt;br /&gt;
print x&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use indexing to fill a matrix with a given shape/function==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot; line &amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
import numpy as np&lt;br /&gt;
&lt;br /&gt;
# 2D example&lt;br /&gt;
size = 10&lt;br /&gt;
extent = 1.0&lt;br /&gt;
Mzeros = np.zeros( (size,size) )&lt;br /&gt;
Mones = np.ones( (size,size) )&lt;br /&gt;
&lt;br /&gt;
axis_x = np.linspace( -extent, +extent, size )&lt;br /&gt;
axis_y = np.linspace( -extent, +extent, size )&lt;br /&gt;
X, Y = np.meshgrid( axis_x, axis_y )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Circle&lt;br /&gt;
radius = 1.0&lt;br /&gt;
T = np.where( (X**2+Y**2)&amp;lt;radius , Mones, Mzeros )&lt;br /&gt;
print T&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# 3D example&lt;br /&gt;
size = 5&lt;br /&gt;
extent = 1.0&lt;br /&gt;
Mzeros = np.zeros( (size,size,size) )&lt;br /&gt;
Mones = np.ones( (size,size,size) )&lt;br /&gt;
&lt;br /&gt;
X, Y, Z = np.mgrid[ -extent:+extent:size*1j , -extent:+extent:size*1j , -extent:+extent:size*1j ]&lt;br /&gt;
&lt;br /&gt;
# Sphere&lt;br /&gt;
radius = 1.0&lt;br /&gt;
T = np.where( (X**2+Y**2+Z**2)&amp;lt;radius , Mones, Mzeros )&lt;br /&gt;
print T&lt;br /&gt;
&lt;br /&gt;
# Superball&lt;br /&gt;
radius = 1.0&lt;br /&gt;
p = 2.1&lt;br /&gt;
threshold = abs(radius)**(2.0*p)&lt;br /&gt;
T = np.where( ( np.power(np.abs(X),2.0*p) + np.power(np.abs(Y),2.0*p) + np.power(np.abs(Z),2.0*p) )&amp;lt;threshold, Mones, Mzeros )&lt;br /&gt;
print T&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use indexing to put 2D/3D array values into a 1D array==&lt;br /&gt;
Example: Azimuthal average: each pixel in the 2D array has a particular bin in the 1D array that it&amp;#039;s supposed to be added to.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot; line &amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
import numpy as np&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# 3D, sphere&lt;br /&gt;
size = 5&lt;br /&gt;
extent = 1.0&lt;br /&gt;
Mzeros = np.zeros( (size,size,size) )&lt;br /&gt;
Mones = np.ones( (size,size,size) )&lt;br /&gt;
&lt;br /&gt;
X, Y, Z = np.mgrid[ -extent:+extent:size*1j , -extent:+extent:size*1j , -extent:+extent:size*1j ]&lt;br /&gt;
&lt;br /&gt;
radius = 1.0&lt;br /&gt;
vals = np.where( (X**2+Y**2+Z**2)&amp;lt;radius , 2.0, 0 )&lt;br /&gt;
&lt;br /&gt;
# Vals are the values we want to put into the array &amp;quot;answer&amp;quot;&lt;br /&gt;
print vals&lt;br /&gt;
&lt;br /&gt;
# Indices are the index values for each &amp;#039;val&amp;#039;, telling us which bin of the &amp;quot;answer&amp;quot; array to put the value in&lt;br /&gt;
indices = np.ones( (size,size,size) )*np.arange(0,size)&lt;br /&gt;
print indices&lt;br /&gt;
&lt;br /&gt;
answer = np.zeros( (size) )&lt;br /&gt;
for idx_cur in range(len(answer)):&lt;br /&gt;
    mask = np.where( indices==idx_cur, 1, 0 )&lt;br /&gt;
&lt;br /&gt;
    print idx_cur, vals*mask&lt;br /&gt;
    &lt;br /&gt;
    answer[idx_cur] = (vals*mask).sum()&lt;br /&gt;
&lt;br /&gt;
print answer&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>KevinYager</name></author>
		
	</entry>
</feed>