<?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>chys&#039;s random notes &#187; MATLAB</title>
	<atom:link href="http://en.chys.info/tag/matlab/feed/" rel="self" type="application/rss+xml" />
	<link>http://en.chys.info</link>
	<description>Study more problems; Talk less of isms.</description>
	<lastBuildDate>Tue, 27 Dec 2011 11:56:38 +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>MATLAB matrices are in column-major storage</title>
		<link>http://en.chys.info/2008/12/matlab-matrices-are-in-column-major-storage/</link>
		<comments>http://en.chys.info/2008/12/matlab-matrices-are-in-column-major-storage/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 05:18:00 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MATLAB]]></category>

		<guid isPermaLink="false">http://blog.chys.info/2008/12/matlab-matrices-are-in-column-major-storage/</guid>
		<description><![CDATA[Authors of each and every high-level programming language claim you only have to care the semantics of its statements without worrying about its internal implementation. Unfortunately, this can hardly be true. Today I was working on some MATLAB codes. One of them scales each row of a sparse matrix to make its 1-norm equal to [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2008/11/matlab-xcb-error/' rel='bookmark' title='MATLAB xcb error'>MATLAB xcb error</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Authors of each and every high-level programming language claim you only have to care the semantics of its statements without worrying about its internal implementation. Unfortunately, this can hardly be true.</p>
<p>Today I was working on some MATLAB codes. One of them scales each row of a sparse matrix to make its <a href="http://en.wikipedia.org/wiki/Vector_norm#p-norm">1-norm</a> equal to 1. The matrix I worked on was 20000×20000, with 153306 nonzero entries.</p>
<p>This is the first version:<br />
<blockquote>
<pre>for k = 1:N
 s = sum(P(k,:));
 if s ~= 0
  P(k,:) = P(k,:)/s;
 end
end</pre>
</blockquote>
<p>This did not stop in 20 seconds, until I pressed Control-C.</p>
<p>This is the second version:<br />
<blockquote>
<pre>P = P';
for k = 1:N
 s = sum(P(:,k));
 if s ~= 0
  P(:,k) = P(:,k)/s;
 end
end
P = P';</pre>
</blockquote>
<p>This finished within one second.</p>
<p>[All entries of matrix P are known to be positive, so I simply use <code>sum</code> to get the 1-norm of a row/column.]</p>
<p>The reason is simple. MATLAB internally stores sparse matrices in <a href="https://people.scs.fsu.edu/~burkardt/data/sparse_cc/sparse_cc.html">compressed column format</a>[1]. Therefore, it is many times more expensive to extract or insert a row than column.</p>
<p>ps. For dense matrices, MATLAB also uses column-major formats, following the FORTRAN convention, though MATLAB is itself written in C (and the GUI in Java) [1].</p>
<p>References</p>
<p>[1] J. Gilbert, C. Moler and R. Schreiber. <a href="http://202.41.85.84/doc/matlab/help/pdf_doc/otherdocs/simax.pdf">Sparse matrices in MATLAB: Design and implementation</a>. SIAM Journal on Matrix Analysis, 1992.
<div class="blogger-post-footer">
<hr />
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://creativecommons.org/images/public/somerights20.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License</a>.</div>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2008/11/matlab-xcb-error/' rel='bookmark' title='MATLAB xcb error'>MATLAB xcb error</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2008/12/matlab-matrices-are-in-column-major-storage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MATLAB xcb error</title>
		<link>http://en.chys.info/2008/11/matlab-xcb-error/</link>
		<comments>http://en.chys.info/2008/11/matlab-xcb-error/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 05:06:00 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MATLAB]]></category>
		<category><![CDATA[X11]]></category>

		<guid isPermaLink="false">http://blog.chys.info/2008/11/matlab-xcb-error/</guid>
		<description><![CDATA[XCB breaks MATLAB (probably also other Java applications): MATLAB: xcb_xlib.c:50: xcb_xlib_unlock: Assertion `c-&#62;xlib.lock&#8217; failed. Either of the two workarounds works for me: (1) If the libxcb version is ≥1.1, export LIBXCB_ALLOW_SLOPPY_LOCK=1 before running the broken application. (2) Recompile libxcb with CFLAGS=&#8221;-DNDEBUG&#8221;. This macro disables assertions. Workaround 2 uses some kind of brutal force, and in [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2008/12/matlab-matrices-are-in-column-major-storage/' rel='bookmark' title='MATLAB matrices are in column-major storage'>MATLAB matrices are in column-major storage</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://xcb.freedesktop.org/">XCB</a> breaks <a href="http://www.mathworks.com/products/matlab/">MATLAB</a> (probably also other Java applications):</p>
<blockquote style="font-family: &quot;Courier New&quot;,Courier,monospace;"><p>MATLAB: xcb_xlib.c:50: xcb_xlib_unlock: Assertion `c-&gt;xlib.lock&#8217; failed.</p></blockquote>
<p>Either of the two workarounds works for me:<br />
(1) If the <span style="font-family: &quot;Courier New&quot;,Courier,monospace;">libxcb</span> version is ≥1.1, export <span style="font-family: &quot;Courier New&quot;,Courier,monospace;">LIBXCB_ALLOW_SLOPPY_LOCK=1</span> before running the broken application.<br />
(2) Recompile <span style="font-family: &quot;Courier New&quot;,Courier,monospace;">libxcb</span> with <span style="font-family: &quot;Courier New&quot;,Courier,monospace;">CFLAGS=&#8221;-DNDEBUG&#8221;</span>. This macro disables <a href="http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.1.html">assertions</a>.</p>
<p>Workaround 2 uses some kind of brutal force, and in most cases Workaround 1 should be preferred.</p>
<p>[ Another workaround is found <a href="http://www.freizeitmoscher.de/?p=14">here</a>. I think it works though I didn’t try it. However this uses even more brutal force. ]</p>
<p>Reference:<br />
[1] <a href="http://forums.gentoo.org/viewtopic-t-554740-highlight-xlib.html">Gentoo Forum thread: xorg-server-1.3.0 + xcb breaks java ?</a><br />
[2] <a href="http://www.linuxfromscratch.org/blfs/view/svn/x/libxcb.html">LFS documentation on libxcb</a>
<div class="blogger-post-footer">
<hr />
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://creativecommons.org/images/public/somerights20.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License</a>.</div>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2008/12/matlab-matrices-are-in-column-major-storage/' rel='bookmark' title='MATLAB matrices are in column-major storage'>MATLAB matrices are in column-major storage</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2008/11/matlab-xcb-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

