<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>What Are You Blogging?</title>
	<atom:link href="http://www.adamlaiacano.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adamlaiacano.com/blog</link>
	<description></description>
	<lastBuildDate>Fri, 08 Jul 2011 00:53:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Visualizing airline carrier delays</title>
		<link>http://www.adamlaiacano.com/blog/2011/06/visualizing-airline-carrier-delays/</link>
		<comments>http://www.adamlaiacano.com/blog/2011/06/visualizing-airline-carrier-delays/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 23:41:43 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.adamlaiacano.com/blog/?p=294</guid>
		<description><![CDATA[I had the worst travel experience of my life recently. I ended up spending as much time delayed at the terminal as I spent in the air: 4 hours down, 3 hours back. I decided to use my time in the lovely Key West International Airport to download the on-time performance data for every major [...]]]></description>
			<content:encoded><![CDATA[<p>I had the worst travel experience of my life recently.  I ended up spending as much time delayed at the terminal as I spent in the air: 4 hours down, 3 hours back. I decided to use my time in the lovely Key West International Airport to download the <a href="http://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=236&amp;DB_Short_Name=On-Time">on-time performance data for every major carrier for 2010 and part of 2011</a> and see what I could find.</p>
<p>I used this wonderful <a href="http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/">flowingdata guide</a> to mapping connections in R and decided to look at the carrier delay time for each route.  The plots below show the average delay from 0 (blue) to 100 (red) minutes for each carrier. For each of the plots, I averaged the delay (or percentage of delays) for each direction of each route.</p>
<p>First, the pretty graphs which I take no credit for designing. I made three graphs for each carrier:</p>
<p>1) Average delay time (these will be low, since most flights have no delay)<br />
2) Percentage of delays of at least 5 minutes<br />
3) Average delay time for flights with at least a 5 minute delay (these will be much larger)</p>
<p>So, here are three airlines. You can click on any of the pictures for slightly larger versions or view <a href="http://www.adamlaiacano.com/airlines">higher quality pdfs here</a> (with other airlines as well, check out the <a href="http://www.adamlaiacano.com/airlines/pinnacle-conditional-delay.pdf">Pinnacle</a> ones).</p>
<p><strong>American Airlines:</strong></p>
<p><strong><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/american-delays.png"><img class="aligncenter size-medium wp-image-295" title="american-delays" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/american-delays-300x300.png" alt="" width="300" height="300" /></a><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/american-perc.png"><img class="aligncenter size-medium wp-image-296" title="american delay percent" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/american-perc-300x300.png" alt="" width="300" height="300" /></a><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/american-conditional-delay.png"><img class="aligncenter size-medium wp-image-297" title="american conditional delay" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/american-conditional-delay-300x300.png" alt="" width="300" height="300" /></a>Delta:</strong></p>
<p><strong><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/delta-delays.png"><img class="aligncenter size-medium wp-image-298" title="delta delays" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/delta-delays-300x300.png" alt="" width="300" height="300" /></a><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/delta-perc.png"><img class="aligncenter size-medium wp-image-299" title="delta percentage" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/delta-perc-300x300.png" alt="" width="300" height="300" /></a><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/delta-conditional-delay.png"><img class="aligncenter size-medium wp-image-300" title="delta-conditional-delay" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/delta-conditional-delay-300x300.png" alt="" width="300" height="300" /></a>JetBlue:</strong></p>
<p><strong><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/jetblue-delays.png"><img class="aligncenter size-medium wp-image-301" title="jetblue delays" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/jetblue-delays-300x300.png" alt="" width="300" height="300" /></a><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/jetblue-perc.png"><img class="aligncenter size-medium wp-image-302" title="jetblue percent" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/jetblue-perc-300x300.png" alt="" width="300" height="300" /></a><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/jetblue-conditional-delay.png"><img class="aligncenter size-medium wp-image-303" title="jetblue-conditional-delay" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/jetblue-conditional-delay-300x300.png" alt="" width="300" height="300" /></a><br />
</strong></p>
<p>There were a lot of generalizations that I made for these. First off, I only looked at the <em>carrier delay</em>, which I assume to mean that it was the airlines fault.  My next step is to look at how each airline handles weather delays, to see which airlines or airports recover best from a big storm, but the scope of this project is pure incompetence. I also didn&#8217;t consider how much of the time is made up for in the air, since a 30-minute delay on a cross-country flight could easily be accounted for by flying a little faster.</p>
<p>For example, there were 2041 American Airlines flights between MIA and LGA. 1166 were MIA-&gt;LGA (average delay: 20.3 minutes), and 875 were LGA-&gt;LGA (average delay: 15.3 minutes).  So the average delay graph will show 18.15 minutes.</p>
<p>Next I looked at the percentage of flights that were delayed by at least five minutes, which I don&#8217;t think requires any explaining.</p>
<p>Lastly, I took a subset of the flights that were delayed by at least 5 minutes (who is REALLY bothered by a 5 minute delay?) and calculated the average of those flights. I really like the graph below that shows the percentage of flights that are still delayed vs. time from the intended take-off. In that plot, you see that Southwest has the most short (&lt; 5 minutes) delays, but the fewest long (&gt; 30 minutes) delays (click for larger).</p>
<p><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/delay-decay.png"><img class="aligncenter size-medium wp-image-304" title="delay-decay" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/06/delay-decay-300x231.png" alt="" width="300" height="231" /></a>All of the data that I used is available <a href="http://www.adamlaiacano.com/airlines/data">here</a> in either R or .csv format. I&#8217;ll make the code less embarrassing and then get it up onto github. I mostly just hacked on the code from the flowingdata post above.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adamlaiacano.com/blog/2011/06/visualizing-airline-carrier-delays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>So, the Sox are 0-6</title>
		<link>http://www.adamlaiacano.com/blog/2011/04/so-the-sox-are-0-6/</link>
		<comments>http://www.adamlaiacano.com/blog/2011/04/so-the-sox-are-0-6/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 19:19:44 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[baseball]]></category>

		<guid isPermaLink="false">http://www.adamlaiacano.com/blog/?p=276</guid>
		<description><![CDATA[This is bad, sure, but at least we aren&#8217;t the 1988 Orioles*. *yet]]></description>
			<content:encoded><![CDATA[<p>This is bad, sure, but at least we aren&#8217;t the 1988 Orioles*.<br />
<a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/worststart.png"><img class="aligncenter size-full wp-image-289" title="worststart" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/worststart.png" alt="" width="600" height="400" /></a></p>
<p>*yet</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adamlaiacano.com/blog/2011/04/so-the-sox-are-0-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My NY Times reading habits before/after paywall</title>
		<link>http://www.adamlaiacano.com/blog/2011/03/my-ny-times-reading-habits-beforeafter-paywall/</link>
		<comments>http://www.adamlaiacano.com/blog/2011/03/my-ny-times-reading-habits-beforeafter-paywall/#comments</comments>
		<pubDate>Fri, 18 Mar 2011 13:24:10 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.adamlaiacano.com/blog/?p=277</guid>
		<description><![CDATA[I&#8217;m wondering how my NY Times reading habits will change once the new paywall comes into effect March 28, limiting the number of free articles per month to 20 (sort of). Most of what I read on the times are three blogs: Economix, Bits, Bitten. I also read the top stories on my iPhone and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m wondering how my NY Times reading habits will change once the <a href="http://www.nytimes.com/content/help/account/purchases/subscriptions-and-purchases.html">new paywall</a> comes into effect March 28, limiting the number of free articles per month to 20 (sort of).</p>
<p>Most of what I read on the times are three blogs: Economix, Bits, Bitten. I also read the top stories on my iPhone and browse around on the <a href="http://www.nytimes.com/skimmer/#/Top+News">article skimmer</a> sometimes.</p>
<p>This graph is from google reader and shows the number of articles posted by a given blog (blue) and how many I&#8217;ve read (orange) per day for the last month:</p>
<p><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/chart-1.png"><img class="aligncenter size-full wp-image-278" title="chart-1" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/chart-1.png" alt="" width="575" height="200" /></a>After the paywall comes into effect, it will look something like this if I don&#8217;t want to subscribe:</p>
<p><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/chart-soon1.png"><img class="aligncenter size-full wp-image-280" title="chart-soon" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/chart-soon1.png" alt="" width="575" height="200" /></a>If all of the regular readers start their 20 article period on the first of every month, it won&#8217;t be long until the blogs post articles with a pattern like the one below (though they obviously won&#8217;t go all the way to zero).</p>
<p><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/chart-eventually.png"><img class="aligncenter size-full wp-image-281" title="chart-eventually" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/chart-eventually.png" alt="" width="575" height="200" /></a>The actual paper would obviously still have stories every day, but the blogs will (or should, at least) post as many articles as possible in the first few days of the month while they&#8217;re still open to the public. In the full paper, the best journalists will try to get their articles posted on the first of the month, which would likely mean the quality of articles would follow a similar pattern.</p>
<p>But here&#8217;s what should save this from happening (where the &#8220;sort of&#8221; comes in). FAQ #12 says:</p>
<blockquote><p><strong>Can I still access NYTimes.com articles through Facebook, Twitter, Google or my blog?</strong></p>
<p>Yes. We encourage links from Facebook, Twitter, search engines, blogs and social media. When you visit NYTimes.com through a link from one of these channels, that article (or video, slide show, etc.) will count toward your monthly limit of 20 free articles, but you will still be able to view it even if you&#8217;ve already read your 20 free articles.</p>
<p>When you visit NYTimes.com by clicking links in Google search results, you&#8217;ll enjoy up to five free articles per day.</p></blockquote>
<p>That could actually increase readership from people like me. Once I pass my 20 free articles, which will probably happen very soon, I will still be able to read 5 articles per day (I&#8217;ll just have to google the titles or something), giving a possible total of 160 free articles in 30 days. Under this system, I&#8217;ll have the incentive to check the site every day instead of coming every so often and reading 40+ articles like I did on 3/10. The graph could potentially take this form:</p>
<p><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/chart-hopeful.png"><img class="aligncenter size-full wp-image-283" title="chart-hopeful" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/chart-hopeful.png" alt="" width="575" height="200" /></a>Making &#8220;check the Times&#8221; part of my daily routine will do one of two things:</p>
<p>1) I&#8217;ll get tired of checking all the time and I&#8217;ll just go back to my current reading habit, but capped at 5 articles per day.<br />
2) I&#8217;ll become more committed and perhaps actually start paying for a subscription.<br />
3) I&#8217;ll just get my news elsewhere.</p>
<p>My guess is that it will be #1.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adamlaiacano.com/blog/2011/03/my-ny-times-reading-habits-beforeafter-paywall/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The all time best and worst basketball teams</title>
		<link>http://www.adamlaiacano.com/blog/2011/03/the-all-time-best-and-worst-basketball-teams/</link>
		<comments>http://www.adamlaiacano.com/blog/2011/03/the-all-time-best-and-worst-basketball-teams/#comments</comments>
		<pubDate>Thu, 17 Mar 2011 19:31:54 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.adamlaiacano.com/blog/?p=269</guid>
		<description><![CDATA[First off, I finally created an account on github, where I&#8217;ll start posting some code. The first thing I put up is a simple webcrawler for downloading the outcome of all of the basketball games since 1970. I&#8217;ll play with the data some more, but for now here are the best and worst teams since [...]]]></description>
			<content:encoded><![CDATA[<p>First off, I finally created an <a href="https://github.com/adamlaiacano" target="_blank">account on github</a>, where I&#8217;ll start posting some code.</p>
<p>The first thing I put up is a simple webcrawler for downloading the outcome of all of the basketball games since 1970. I&#8217;ll play with the data some more, but for now here are the best and worst teams since 1970, and the average score difference.</p>
<p>By win/loss record:</p>
<pre>team year wins losses   avg.win
 CHI 1996   72     10 11.940000
 CHI 1997   69     13  9.811881
 LAL 1972   69     13 10.876289
 BOS 1973   68     14  7.294737
 BOS 1986   67     15  9.580000
 CHI 1992   67     15  9.538462</pre>
<pre>team year wins losses    avg.win
 DAL 1994   13     69  -8.695122
 LAC 1987   12     70 -11.426829
 NJN 2010   12     70  -9.121951
 DAL 1993   11     71 -15.195122
 DEN 1998   11     71 -11.780488
 PHI 1973    9     73 -12.085366</pre>
<p>And by the average score difference:</p>
<pre>team year wins losses   avg.win
 MIL 1971   66     16 12.583333
 CHI 1996   72     10 11.940000
 LAL 1972   69     13 10.876289
 MIL 1972   63     19 10.602151
 CHI 1997   69     13  9.811881
 LAL 1987   65     17  9.680000</pre>
<pre>team year wins losses   avg.win
 LAC 1987   12     70 -11.42683
 LAC 2000   15     67 -11.52439
 HOU 1983   14     68 -11.59756
 DEN 1998   11     71 -11.78049
 PHI 1973    9     73 -12.08537
 DAL 1993   11     71 -15.19512</pre>
<p>The next thing to do is figure out who these teams are who are scoring more points than they should be, but for now I&#8217;m off to dinner.<br />
<a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/plot.png"><img class="aligncenter size-full wp-image-274" title="plot" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/plot.png" alt="" width="500" height="400" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adamlaiacano.com/blog/2011/03/the-all-time-best-and-worst-basketball-teams/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Statistical software use at Columbia Biz School</title>
		<link>http://www.adamlaiacano.com/blog/2011/03/statistical-software-use-at-columbia-biz-school/</link>
		<comments>http://www.adamlaiacano.com/blog/2011/03/statistical-software-use-at-columbia-biz-school/#comments</comments>
		<pubDate>Wed, 02 Mar 2011 22:33:16 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.adamlaiacano.com/blog/?p=258</guid>
		<description><![CDATA[I got ahold of the Sun Grid Engine logs for our server at work and I&#8217;ll hopefully figure out  some ways to get our computing cluster running more efficiently. The image below shows which programs are used the most by CBS professors. There are some pretty strong biases to the plot: This is only data [...]]]></description>
			<content:encoded><![CDATA[<p>I got ahold of the Sun Grid Engine logs for our server at work and I&#8217;ll hopefully figure out  some ways to get our computing cluster running more efficiently.</p>
<p>The image below shows which programs are used the most by CBS professors. There are some pretty strong biases to the plot:</p>
<ol>
<li>This is only data from our computing cluster, so desktop use or use on other servers is not included. My work, for example, is not well represented on here (I&#8217;m the circle in the second graph).</li>
<li>All of our datasets on this server are stored in SAS format, so if you want to access them you&#8217;re going to have to run SAS at least once. I have a handy &#8220;get out of sas&#8221; script.</li>
<li>Some programs are more interactive and only need to be opened once to do all of your work. That&#8217;s probably why the number of program launches for Stata and Matlab are lower than SAS even though there are more unique users.</li>
<li>I had to exclude python, java, bash, etc. scripts because they are run so many times for non-computing purposes. I also excluded custom compiled programs (mainly in C++) because they have unique names and are only launched a few times each.</li>
</ol>
<p><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/appsbyuser_large.png"><img class="aligncenter size-full wp-image-259" title="appsbyuser_small" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/appsbyuser_small.png" alt="" width="600" height="400" /></a></p>
<p>One thing I find very interesting and would love to have someone explain is the linear relationships between memory usage and CPU.  I talked to the &#8220;star&#8221; user in the plot below about this and looked at the dates, and he said that the lines correspond to two different projects. Anyone have any references on memory/processor tradeoffs and whether or not there&#8217;s an advantage to a steep or narrow slope in a plot like this?</p>
<p><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/performance_large.png"><img class="aligncenter size-full wp-image-262" title="performance_small" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/03/performance_small.png" alt="" width="500" height="400" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adamlaiacano.com/blog/2011/03/statistical-software-use-at-columbia-biz-school/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Way to go, Charlie Sheen!</title>
		<link>http://www.adamlaiacano.com/blog/2011/02/way-to-go-charlie-sheen/</link>
		<comments>http://www.adamlaiacano.com/blog/2011/02/way-to-go-charlie-sheen/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 14:46:39 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.adamlaiacano.com/blog/?p=247</guid>
		<description><![CDATA[That&#8217;s him up there pulling in $1.25M per episode of the worst/most popular comedy on TV. And to add to the insanity, the second highest paid star is Jon Cryer from the same show at $550k per episode. And here it is with names. I don&#8217;t have time to fix the overlap right now. h/t [...]]]></description>
			<content:encoded><![CDATA[<p>That&#8217;s him up there pulling in $1.25M per episode of the worst/most popular comedy on TV. And to add to the insanity, the second highest paid star is Jon Cryer from the same show at $550k per episode.</p>
<p><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/02/payperepisode.png"><img class="aligncenter size-full wp-image-248" title="payperepisode" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/02/payperepisode.png" alt="" width="500" height="600" /></a></p>
<p>And here it is with names. I don&#8217;t have time to fix the overlap right now.</p>
<p><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/02/payperactor.png"><img class="aligncenter size-full wp-image-254" title="payperactor" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/02/payperactor.png" alt="" width="500" height="600" /></a></p>
<p>h/t <a href="http://flowingdata.com/2011/02/15/visualize-this-tvs-top-earners/">Flowing Data</a> for the data.</p>
<p>[<strong>EDIT</strong>: <em>I posted this in February, before Charlie Sheen became Chief Douchebag of the media, though probably well after he became an actual douchebag. I would certainly have a different title for this post today</em>]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adamlaiacano.com/blog/2011/02/way-to-go-charlie-sheen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get wikipedia URL for any search term</title>
		<link>http://www.adamlaiacano.com/blog/2011/02/get-wikipedia-url-for-any-search-term/</link>
		<comments>http://www.adamlaiacano.com/blog/2011/02/get-wikipedia-url-for-any-search-term/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 22:03:00 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.adamlaiacano.com/blog/?p=244</guid>
		<description><![CDATA[This requires a (free) Yahoo! BOSS account.  It&#8217;s pretty self-explanatory, otherwise. def get_wikipedia_url&#40;query, boss_id, api = False&#41;: &#34;&#34;&#34; Takes a search query and returns the wikipedia link in the top results. &#160; If there's no wikipedia link, it returns None. &#160; Use api = True if you want to get the link to the API [...]]]></description>
			<content:encoded><![CDATA[<p>This requires a (free) <a href="http://developer.yahoo.com/search/boss/">Yahoo! BOSS</a> account.  It&#8217;s pretty self-explanatory, otherwise.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> get_wikipedia_url<span style="color: black;">&#40;</span>query, boss_id, api = <span style="color: #008000;">False</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Takes a search query and returns the wikipedia link in the top results.
&nbsp;
    If there's no wikipedia link, it returns None.
&nbsp;
    Use api = True if you want to get the link to the API page rather than
    normal web page.
&nbsp;
    @param[in] query Search query
    @param[in] boss_id Your Yahoo! BOSS ID.
    @param[in] api Boolean (false by default) to return the API url rather
        than web url
    @param[out] url First wikipedia result, or None if not found
    &quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">from</span> json <span style="color: #ff7700;font-weight:bold;">import</span> loads
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">re</span> <span style="color: #ff7700;font-weight:bold;">import</span> sub, search
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">urllib2</span> <span style="color: #ff7700;font-weight:bold;">import</span> urlopen
&nbsp;
    query = sub<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; &quot;</span>, <span style="color: #483d8b;">&quot;+&quot;</span>, query<span style="color: black;">&#41;</span>
    url = <span style="color: #483d8b;">&quot;http://boss.yahooapis.com/ysearch/web/v1/&quot;</span>+query+<span style="color: #483d8b;">&quot;?appid=&quot;</span>+boss_id+<span style="color: #483d8b;">&quot;&amp;format=json&quot;</span>
&nbsp;
    boss = loads<span style="color: black;">&#40;</span>urlopen<span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> url <span style="color: #ff7700;font-weight:bold;">in</span> boss<span style="color: black;">&#91;</span><span style="color: #483d8b;">'ysearchresponse'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'resultset_web'</span><span style="color: black;">&#93;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> search<span style="color: black;">&#40;</span><span style="color: #483d8b;">'en<span style="color: #000099; font-weight: bold;">\.</span>wikipedia<span style="color: #000099; font-weight: bold;">\.</span>org'</span>, url<span style="color: black;">&#91;</span><span style="color: #483d8b;">'url'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> api == <span style="color: #008000;">True</span>:
                page = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#91;</span><span style="color: #483d8b;">'url'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;http://en.wikipedia.org/w/api.php?action=query&amp;format=json&amp;rvprop=content&amp;prop=revisions&amp;redirects=1&amp;titles=&quot;</span>+page
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#91;</span><span style="color: #483d8b;">'url'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.adamlaiacano.com/blog/2011/02/get-wikipedia-url-for-any-search-term/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Today&#8217;s worst infographic.</title>
		<link>http://www.adamlaiacano.com/blog/2011/02/todays-worst-infographic/</link>
		<comments>http://www.adamlaiacano.com/blog/2011/02/todays-worst-infographic/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 23:13:09 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.adamlaiacano.com/blog/?p=240</guid>
		<description><![CDATA[A friend of mine just shared this picture on google reader: Let me describe the reasons I hate it: It appears that all of the US presidents were in office the same amount of time. George HW Bush should be half the size and Obama should be 1/4 the size. All of the dates are evenly [...]]]></description>
			<content:encoded><![CDATA[<p>A friend of mine just shared <a href="http://www.likecool.com/Presidents--Pic--Gear.html">this picture</a> on google reader:</p>
<p><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/02/Presidents-Pic-Gear.jpeg"><img class="aligncenter size-full wp-image-241" title="Presidents" src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/02/Presidents-Pic-Gear.jpeg" alt="" width="580" height="500" /></a></p>
<p>Let me describe the reasons I hate it:</p>
<ol>
<li>It appears that all of the US presidents were in office the same amount of time. George HW Bush should be half the size and Obama should be 1/4 the size.</li>
<li>All of the dates are evenly spaced, but one of them is only a 4 year difference.</li>
<li>All of the presidential terms are off. It makes it look like George W. Bush&#8217;s term ran from 1993 to 2001, when in fact it ran from 2001 to 2008.</li>
<li>The timeline ends in (presumably the beginning of) 2009. Obama shouldn&#8217;t even be on there.</li>
<li>If the point of the graphic is to show that Hosni Mubarak was in power through 5 US presidential administrations, and that Japan has had many leaders, there should only be one picture of Mubarak.</li>
<li>I don&#8217;t know if a 30 year term is typical for Egypt or not. How long was the previous leader in office? (11 years, with his term ending in assassination)? How about the guy before that? (14 years). They should have put up at least one other guy on there.</li>
</ol>
<p>Based on all of these disasters, I know absolutely nothing about what<br />
was going on in Japan. Have they really had 5 leaders in the 2 (or is<br />
it 8) years that Obama has been president?</p>
<p>&lt;/rant&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adamlaiacano.com/blog/2011/02/todays-worst-infographic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;Create yours now&#8221;</title>
		<link>http://www.adamlaiacano.com/blog/2011/01/create-yours-now/</link>
		<comments>http://www.adamlaiacano.com/blog/2011/01/create-yours-now/#comments</comments>
		<pubDate>Sat, 08 Jan 2011 21:19:33 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.adamlaiacano.com/blog/?p=202</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/01/photo.jpeg"><img src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/01/photo.jpeg" alt="" title="photo" width="315" height="264" class="aligncenter size-full wp-image-237" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adamlaiacano.com/blog/2011/01/create-yours-now/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>POSIX threading within Matlab mex functions</title>
		<link>http://www.adamlaiacano.com/blog/2011/01/posix-threading-within-matlab-mex-functions/</link>
		<comments>http://www.adamlaiacano.com/blog/2011/01/posix-threading-within-matlab-mex-functions/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 01:53:18 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.adamlaiacano.com/blog/?p=203</guid>
		<description><![CDATA[I got thrown into writing multi-threaded programs at work when I had to port some code from Visual C++ to GNU C++. Some grad student wrote the original code and I just had to modify it, which was annoying but it gave me a minimal working knowledge of POSIX threading, which is actually far easier [...]]]></description>
			<content:encoded><![CDATA[<p>I got thrown into writing multi-threaded programs at work when I had to port some code from Visual C++ to GNU C++.  Some grad student wrote the original code and I just had to modify it, which was annoying but it gave me a minimal working knowledge of <a href="https://computing.llnl.gov/tutorials/pthreads/" target="_new">POSIX threading</a>, which is actually far easier than I expected. </p>
<p>While Matlab does a good job of using multiple processor cores on some of its built-in functions, I wanted to get add that to some of the custom <a href="http://www.shawnlankton.com/2008/03/getting-started-with-mex-a-short-tutorial/" target="_new">mex functions</a> I&#8217;ve written.  I&#8217;ve achieved some great speed increases when re-writing matlab functions in c. Especially with iterative loops (<tt>x(i) = x(i-1)+x(i+1)</tt>), or times where I can make enormous assumptions about the input to a function and avoid all kinds of time-wasting error detections.</p>
<p>Anyway, I spent a little time trying to figure out how to use threading within a matlab mex function and was able to get it working.  The actual code is below, and I assume some familiarity with mex functions and POSIX threading (read the links above). Here&#8217;s a brief overview of how it works:</p>
<li>The function called by each thread must be of the form <tt>void *function_name(*void)</tt>.</li>
<li>You can only send one variable to the function, so if you want to send more information than that, create a <tt>struct</tt> to hold everything. When you create the thread, you&#8217;ll have to cast this as a <tt>void*</tt>.</li>
<li>Once in the thread function, you need to create a pointer of the type that you want to use (the <tt>struct</tt> that you just made) to the <tt>void*</tt> that was passed to it.</li>
<p>The code below simply fills a matrix with this very exciting pattern:<br />
<a href="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/01/file.png"><img src="http://www.adamlaiacano.com/blog/wp-content/uploads/2011/01/file.png" alt="" title="matrix pattern" width="576" height="432" class="aligncenter size-full wp-image-214" /></a></p>
<p>Within the <tt>mexFunction()</tt>, I figure out how many columns of this matrix will be filled by each thread (always try to process by column. See the bottom of this post for more).  In my case, I used 7 threads and had a 1000&#215;1000 matrix, so each of the first 6 threads process 142 columns, and the 7th thread processes the remaining 148.  I realize it&#8217;s better to have 6 threads process 143 and the 7th process 142, but I didn&#8217;t really want to get into figuring that out right now.</p>
<p>You can compile and run the code with two simple lines. Make sure that when you compile it, you link against the <tt>pthread</tt> library.  In this example I have each thread print out it&#8217;s ID and the number of columns it will be working on.</p>
<pre>
&gt;&gt; mex -lpthread fill_matrix_par.c
&gt;&gt; x=fill_matrix_par(1000);
In thread 0. Filling 142 columns
In thread 6. Filling 148 columns
In thread 4. Filling 142 columns
In thread 3. Filling 142 columns
In thread 2. Filling 142 columns
In thread 1. Filling 142 columns
</pre>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;mex.h&quot;</span>
<span style="color: #339933;">#include &lt;math.h&gt;</span>
<span style="color: #339933;">#include &quot;pthread.h&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * prhs[0] : Nrow - dimension of the square matrix to be output
 *
 * plhs[0] : CS   - an Nrow by Nrow matrix output
 */</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* We need to define a structure to pass to each thread, since you can only
 * pass it one variable.  This will hold everything needed by the thread.
 */</span>
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #993333;">int</span> Ncol<span style="color: #339933;">;</span>           <span style="color: #808080; font-style: italic;">/* number of columns each specific thread will process */</span>
   <span style="color: #993333;">int</span> Nrow<span style="color: #339933;">;</span>           <span style="color: #808080; font-style: italic;">/* number of rows in the matrix */</span>
   <span style="color: #993333;">int</span> offset<span style="color: #339933;">;</span>         <span style="color: #808080; font-style: italic;">/* this is for use in filling the matrix */</span>
   <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>              <span style="color: #808080; font-style: italic;">/* this is just and ID for the thread */</span>
   <span style="color: #993333;">double</span> <span style="color: #339933;">*</span>col_start<span style="color: #339933;">;</span>  <span style="color: #808080; font-style: italic;">/* This is a pointer to a specific section of the matrix */</span> 
<span style="color: #009900;">&#125;</span> info_T<span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* Define how many threads you want to use. 
 * This shouldn't exceed the number of cores on your computer. I'm using
 * an 8-core server, so I'll use 7 of them for now
 */</span>
<span style="color: #339933;">#define MAX_NUMBER_THREADS 7</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Here's the definition of the function that will be called 
 * in each thread
 */</span>
<span style="color: #993333;">void</span> <span style="color: #339933;">*</span>calc_col<span style="color: #009900;">&#40;</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>ptr <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #993333;">void</span> mexFunction<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> nlhs<span style="color: #339933;">,</span>
            mxArray<span style="color: #339933;">*</span> plhs<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
            <span style="color: #993333;">int</span> nrhs<span style="color: #339933;">,</span>
            <span style="color: #993333;">const</span> mxArray<span style="color: #339933;">*</span> prhs<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Grab the input from matlab */</span>
    <span style="color: #993333;">int</span> Nrow <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>mxGetPr<span style="color: #009900;">&#40;</span>prhs<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Create the output DoubleMatrix */</span>
    mxArray<span style="color: #339933;">*</span> CSarr  <span style="color: #339933;">=</span> mxCreateDoubleMatrix<span style="color: #009900;">&#40;</span>Nrow<span style="color: #339933;">,</span> Nrow<span style="color: #339933;">,</span> mxREAL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">double</span><span style="color: #339933;">*</span> CS <span style="color: #339933;">=</span> mxGetPr<span style="color: #009900;">&#40;</span>CSarr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* An array of threads and structures to be passed to each thread */</span>
    pthread_t pThreads<span style="color: #009900;">&#91;</span>MAX_NUMBER_THREADS<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    info_T pass_struct<span style="color: #009900;">&#91;</span>MAX_NUMBER_THREADS<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Each thread will handle this many columns, except the last thread
     * will will handle the remainder as well.  I could speed this up by
     * evenly distributing the remaineder threads, but I don't.
     */</span>
    <span style="color: #993333;">int</span> cols_per_thread <span style="color: #339933;">=</span> Nrow <span style="color: #339933;">/</span> MAX_NUMBER_THREADS<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> MAX_NUMBER_THREADS<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #808080; font-style: italic;">/* fill the structure with info to be passed to the given thread */</span>
        pass_struct<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #202020;">Ncol</span>      <span style="color: #339933;">=</span> cols_per_thread<span style="color: #339933;">;</span>
        pass_struct<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #202020;">Nrow</span>      <span style="color: #339933;">=</span> Nrow<span style="color: #339933;">;</span>
        pass_struct<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #202020;">col_start</span> <span style="color: #339933;">=</span> CS <span style="color: #339933;">+</span> Nrow<span style="color: #339933;">*</span>i<span style="color: #339933;">*</span>cols_per_thread<span style="color: #339933;">;</span>
        pass_struct<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #202020;">i</span>         <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
        pass_struct<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #202020;">offset</span>    <span style="color: #339933;">=</span> i<span style="color: #339933;">*</span>cols_per_thread<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">/* The last thread needs to get the remainder of the columns.
         */</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> MAX_NUMBER_THREADS <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            pass_struct<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #202020;">Ncol</span> <span style="color: #339933;">=</span> Nrow <span style="color: #339933;">-</span> i<span style="color: #339933;">*</span>cols_per_thread<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">/* spawn the thread */</span>
        pthread_create<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>pThreads<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> 
                calc_col<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>pass_struct<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* wait for all of the threads to finish */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> MAX_NUMBER_THREADS<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        pthread_join<span style="color: #009900;">&#40;</span> pThreads<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* return CSarr to matlab */</span>
    nlhs <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    plhs<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> CSarr<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #993333;">void</span> <span style="color: #339933;">*</span>calc_col<span style="color: #009900;">&#40;</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>ptr <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* Here's the function that calculates the column starting at the 
     * given point 
     */</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* create a local structure to point to the input pointer */</span>
    info_T <span style="color: #339933;">*</span>struct_ptr<span style="color: #339933;">;</span>
    struct_ptr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>info_T <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> ptr<span style="color: #339933;">;</span>
&nbsp;
    mexPrintf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;In thread %i. Filling %i columns<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> 
            struct_ptr<span style="color: #339933;">-&gt;</span>i<span style="color: #339933;">,</span> struct_ptr<span style="color: #339933;">-&gt;</span>Ncol<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* perform whatever you need to do on the matrix here */</span>
    <span style="color: #993333;">int</span> rows<span style="color: #339933;">,</span> cols<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>cols<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> cols<span style="color: #339933;">&lt;</span>struct_ptr<span style="color: #339933;">-&gt;</span>Ncol<span style="color: #339933;">;</span> cols<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>rows<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> rows<span style="color: #339933;">&lt;</span>struct_ptr<span style="color: #339933;">-&gt;</span>Nrow<span style="color: #339933;">;</span> rows<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>struct_ptr<span style="color: #339933;">-&gt;</span>col_start <span style="color: #339933;">+</span> rows <span style="color: #339933;">+</span> cols<span style="color: #339933;">*</span>struct_ptr<span style="color: #339933;">-&gt;</span>Nrow<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> 
                    <span style="color: #339933;">-</span>rows<span style="color: #339933;">+</span>cols<span style="color: #339933;">+</span>struct_ptr<span style="color: #339933;">-&gt;</span>offset<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* exit the thread */</span>
    pthread_exit<span style="color: #009900;">&#40;</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><b>So here&#8217;s the catch:</b> Each of the threads are writing to the <i>same matrix at the same time</i>, each accessing a different section of the memory. <tt>struct_ptr->col_start</tt> points to column 0, 142, 244, &#8230; depending on the thread number. </p>
<p>This is PROBABLY a bad idea and I don&#8217;t think it&#8217;s normally considered to be thread-safe, but it works here. I most often write specific functions for specific tasks that fit inside specific Matlab scripts.  I&#8217;d love for someone to correct me on this, but for now I just figured it out on my own.</p>
<hr />
<p>With regards to processing by column instead of row, it&#8217;s always better to work down columns because of how the arrays are stored in memory.  The image below sums it up pretty well:</p>
<p><a target="_new" href="http://software-carpentry.org/4_0/matrix/basics/"><img src="http://software-carpentry.org/lectures/matrix/matrix-basics/slide-025.png" border=0></a></p>
<p>So if you want to fill a matrix by row in c/c++, you would need to use two loops and do lots of multiplying:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>r<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> r <span style="color: #339933;">&lt;</span> Nrows<span style="color: #339933;">;</span> r<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>c<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>c <span style="color: #339933;">&lt;</span> Ncols<span style="color: #339933;">;</span> c<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>array_loc1 <span style="color: #339933;">+</span> c<span style="color: #339933;">*</span>Nrows <span style="color: #339933;">+</span> r<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> c<span style="color: #339933;">+</span>r<span style="color: #339933;">*</span>Nrows<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and get this for an output:</p>
<pre>
     0     1     2     3     4
     5     6     7     8     9
    10    11    12    13    14
    15    16    17    18    19
    20    21    22    23    24
</pre>
<p>Whereas to fill it by column you would just loop over the total number of cells:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>c<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> c <span style="color: #339933;">&lt;</span> Nrows <span style="color: #339933;">*</span> Ncols<span style="color: #339933;">;</span> c<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>array_loc2 <span style="color: #339933;">+</span> c<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> c<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and you&#8217;ll get this:</p>
<pre>
     0     5    10    15    20
     1     6    11    16    21
     2     7    12    17    22
     3     8    13    18    23
     4     9    14    19    24
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.adamlaiacano.com/blog/2011/01/posix-threading-within-matlab-mex-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

