<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Making Functions Suitable for ND Arrays</title>
	<atom:link href="http://blogs.mathworks.com/loren/2006/03/22/making-functions-suitable-for-nd-arrays/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2006/03/22/making-functions-suitable-for-nd-arrays/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/03/22/making-functions-suitable-for-nd-arrays/#comment-31838</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 12 Nov 2010 20:57:07 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=28#comment-31838</guid>
		<description>Arthur-

I agree that automatic conversion from string to double, esp. obvious in the case of &quot;:&quot;, causes some very unpleasant issues.  People here are listening, though there is no immediate solution.  Thanks for commenting.

--Loren</description>
		<content:encoded><![CDATA[<p>Arthur-</p>
<p>I agree that automatic conversion from string to double, esp. obvious in the case of &#8220;:&#8221;, causes some very unpleasant issues.  People here are listening, though there is no immediate solution.  Thanks for commenting.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arthur</title>
		<link>http://blogs.mathworks.com/loren/2006/03/22/making-functions-suitable-for-nd-arrays/#comment-31833</link>
		<dc:creator>Arthur</dc:creator>
		<pubDate>Thu, 11 Nov 2010 21:59:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=28#comment-31833</guid>
		<description>The biggest problem with how MATLAB uses &#039;:&#039; in my opinion is how easy it is for a user-defined subsasgn() to fail in mysterious ways. Since MATLAB silently promotes chars to doubles, the developer has to remember to check for &quot;ischar(S(1).subs{n}) &amp;&amp; isequal(S(1).subs{n}, &#039;:&#039;)&quot;, otherwise the dreaded element 58 sneaks through.

I would be thrilled to see implicit type conversion from char to double disappear in a future version of MATLAB. That has been a source of many bugs in my experience.</description>
		<content:encoded><![CDATA[<p>The biggest problem with how MATLAB uses &#8216;:&#8217; in my opinion is how easy it is for a user-defined subsasgn() to fail in mysterious ways. Since MATLAB silently promotes chars to doubles, the developer has to remember to check for &#8220;ischar(S(1).subs{n}) &amp;&amp; isequal(S(1).subs{n}, &#8216;:&#8217;)&#8221;, otherwise the dreaded element 58 sneaks through.</p>
<p>I would be thrilled to see implicit type conversion from char to double disappear in a future version of MATLAB. That has been a source of many bugs in my experience.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/03/22/making-functions-suitable-for-nd-arrays/#comment-31831</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 10 Nov 2010 20:00:47 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=28#comment-31831</guid>
		<description>Jonathan-

That&#039;s just not part of indexing with MATLAB.  The end has to be used directly in an expression so the context can be discerned.  Though you can index with strings in MATLAB, sort of, the only string MATLAB really copes with is :.  This has caused problems for some because double(&#039;:&#039;) is 58 and that can cause some anomalies in code that I think of as strangely written (though others may disagree).

--Loren</description>
		<content:encoded><![CDATA[<p>Jonathan-</p>
<p>That&#8217;s just not part of indexing with MATLAB.  The end has to be used directly in an expression so the context can be discerned.  Though you can index with strings in MATLAB, sort of, the only string MATLAB really copes with is :.  This has caused problems for some because double(&#8216;:&#8217;) is 58 and that can cause some anomalies in code that I think of as strangely written (though others may disagree).</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan</title>
		<link>http://blogs.mathworks.com/loren/2006/03/22/making-functions-suitable-for-nd-arrays/#comment-31830</link>
		<dc:creator>Jonathan</dc:creator>
		<pubDate>Wed, 10 Nov 2010 19:53:10 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=28#comment-31830</guid>
		<description>I wish cell of indices method supported use of &#039;end&#039; and step sizes as in &lt;pre&gt;idx={&#039;:&#039;,&#039;1:2:end&#039;}
A(idx{:});
&lt;/pre&gt; 
Maybe I&#039;m doing something wrong but I get errors with this formulation.</description>
		<content:encoded><![CDATA[<p>I wish cell of indices method supported use of &#8216;end&#8217; and step sizes as in
<pre>idx={':','1:2:end'}
A(idx{:});
</pre>
</p><p>Maybe I&#8217;m doing something wrong but I get errors with this formulation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Akiel</title>
		<link>http://blogs.mathworks.com/loren/2006/03/22/making-functions-suitable-for-nd-arrays/#comment-30482</link>
		<dc:creator>Akiel</dc:creator>
		<pubDate>Thu, 23 Jul 2009 11:58:10 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=28#comment-30482</guid>
		<description>hmm, I didn&#039;t know of bsxfun() until this moment. Apparently it does the sort of thing I want, but I&#039;ll have to play with it and see how it works.</description>
		<content:encoded><![CDATA[<p>hmm, I didn&#8217;t know of bsxfun() until this moment. Apparently it does the sort of thing I want, but I&#8217;ll have to play with it and see how it works.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Akiel</title>
		<link>http://blogs.mathworks.com/loren/2006/03/22/making-functions-suitable-for-nd-arrays/#comment-30478</link>
		<dc:creator>Akiel</dc:creator>
		<pubDate>Sun, 19 Jul 2009 15:39:28 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=28#comment-30478</guid>
		<description>Hi Loren,

In my work as a research student in image processing, I frequently have to deal with some sort of operation between two arrays of different number of dimensions (usually differing by one, but of the same size for the common dimensions) that seems natural enough to me to warrant having a simple way of implementing these operations, but currently either requiring a loop or the permute/reshape/repmat approach (that I originally learnt from Peter Acklam&#039;s cool Matlab tips and tricks Guide).

I don&#039;t know what the people over at MATLAB would think of this idea, or whether they&#039;ve already considered something like this: introduce a special type of array that uses a special single-dimensional &quot;modulus&quot; index, e.g., if A is an array of this special type, of size m x n, then
A[x] and A[y] where x = rem(y-1, m*n)+1 refer to the same element. So if A is of size 3 x 4, for example, A[13] winds back to A[1], and so does A[25], etc. 

Then operations between arrays of dimensions, for example, dim and dim-1, can be defined as operations between a &quot;normal&quot; array and the special array respectively. 
So if I had M of size pxqxr and special array N of size pxq respectively (N being special), then P = M*N would matrix multiply each 2D slice of P with N, and P = M.*N would take the element-by-element product for each 2D slice of P with N, etc.

Admittedly I haven&#039;t thought this through and my approach may be rubbish (the idea came to me just now, and I&#039;m not really the sharpest knife in any drawer), but really the important thing for me is, I feel MATLAB really need to come up with a better way to handle operations between ND-arrays, and arrays of different numbers of dimension.</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>In my work as a research student in image processing, I frequently have to deal with some sort of operation between two arrays of different number of dimensions (usually differing by one, but of the same size for the common dimensions) that seems natural enough to me to warrant having a simple way of implementing these operations, but currently either requiring a loop or the permute/reshape/repmat approach (that I originally learnt from Peter Acklam&#8217;s cool Matlab tips and tricks Guide).</p>
<p>I don&#8217;t know what the people over at MATLAB would think of this idea, or whether they&#8217;ve already considered something like this: introduce a special type of array that uses a special single-dimensional &#8220;modulus&#8221; index, e.g., if A is an array of this special type, of size m x n, then<br />
A[x] and A[y] where x = rem(y-1, m*n)+1 refer to the same element. So if A is of size 3 x 4, for example, A[13] winds back to A[1], and so does A[25], etc. </p>
<p>Then operations between arrays of dimensions, for example, dim and dim-1, can be defined as operations between a &#8220;normal&#8221; array and the special array respectively.<br />
So if I had M of size pxqxr and special array N of size pxq respectively (N being special), then P = M*N would matrix multiply each 2D slice of P with N, and P = M.*N would take the element-by-element product for each 2D slice of P with N, etc.</p>
<p>Admittedly I haven&#8217;t thought this through and my approach may be rubbish (the idea came to me just now, and I&#8217;m not really the sharpest knife in any drawer), but really the important thing for me is, I feel MATLAB really need to come up with a better way to handle operations between ND-arrays, and arrays of different numbers of dimension.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/03/22/making-functions-suitable-for-nd-arrays/#comment-29952</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 08 Jan 2009 14:41:21 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=28#comment-29952</guid>
		<description>Johann-

You have to permute the arrays to be truly 2-D to apply the operations (using permute, reshape, or squeeze perhaps).

--Loren</description>
		<content:encoded><![CDATA[<p>Johann-</p>
<p>You have to permute the arrays to be truly 2-D to apply the operations (using permute, reshape, or squeeze perhaps).</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: johann</title>
		<link>http://blogs.mathworks.com/loren/2006/03/22/making-functions-suitable-for-nd-arrays/#comment-29950</link>
		<dc:creator>johann</dc:creator>
		<pubDate>Wed, 07 Jan 2009 17:00:47 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=28#comment-29950</guid>
		<description>Hello Loren Shure,

suppose there is a 3-dim ND array A of size n,3,2.
For each 1&lt;=j&lt;=n I interpret the &quot;level&quot; A(j,:,:) as a 3x2 matrix, but then matlab does perform computations for these &quot;level-matrices&quot;. In my particular case I aim at computing A(j,:,:)*A(j,:,:)&#039;</description>
		<content:encoded><![CDATA[<p>Hello Loren Shure,</p>
<p>suppose there is a 3-dim ND array A of size n,3,2.<br />
For each 1&lt;=j&lt;=n I interpret the &#8220;level&#8221; A(j,:,:) as a 3&#215;2 matrix, but then matlab does perform computations for these &#8220;level-matrices&#8221;. In my particular case I aim at computing A(j,:,:)*A(j,:,:)&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sridharan kamalakannan</title>
		<link>http://blogs.mathworks.com/loren/2006/03/22/making-functions-suitable-for-nd-arrays/#comment-16604</link>
		<dc:creator>sridharan kamalakannan</dc:creator>
		<pubDate>Tue, 04 Sep 2007 02:39:53 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=28#comment-16604</guid>
		<description>Hi Loren,

I have a stack of 2D polygons. Can u suggest me a way to stack these polygons in 3D such that I get a 3D object out of these 2D polygons.</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>I have a stack of 2D polygons. Can u suggest me a way to stack these polygons in 3D such that I get a 3D object out of these 2D polygons.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: C Rose</title>
		<link>http://blogs.mathworks.com/loren/2006/03/22/making-functions-suitable-for-nd-arrays/#comment-4188</link>
		<dc:creator>C Rose</dc:creator>
		<pubDate>Fri, 28 Jul 2006 09:14:52 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=28#comment-4188</guid>
		<description>I&#039;ve recently implemented some code to handle arbitrary-dimensional data, and used yet another approach.

My solution is perhaps a bit &#039;hackish&#039;, but it works quite well. I wanted to be able to index an array called obj in the following ways (example is with obj being 2D):

obj(1:2:end, 1:2:end)
obj(1:2:end, 2:2:end)
obj(2:2:end, 1:2:end)
obj(2:2:end, 2:2:end)

For 3D it would be obj(1:2:end, 1:2:end, 1:2:end) etc. and there would be 2^3 combinations.

I used base-2 counting (via Matlab&#039;s DEC2BIN) to enumerate the combinations and the actual indexing on obj was achieved by creating a string (e.g. &#039;obj(1:2:end, 1:2:end)&#039;) and then calling Matlab&#039;s EVAL on that string.

The advantages of this method are: 

one doesn&#039;t have to learn a new syntax for indexing (I&#039;ve been using Matlab for years and didn&#039;t know of Loren&#039;s cell array-based method, and it wouldn&#039;t have occurred to me);

by generating strings, which are then evaluated, it&#039;s easy to see exactly what would be computed (though actually the cell array method is equally transparent once you know about it).

I imagined the main disadvantage would be the performance hit of making Matlab to interpret code at run-time, however profiling on simple examples shows that there doesn&#039;t appear to be a massive performance hit compared to some of the other Matlab functions I&#039;m using (e.g. REGRESS, PADARRAY) in the code.

It would be interesting to know more about EVAL.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve recently implemented some code to handle arbitrary-dimensional data, and used yet another approach.</p>
<p>My solution is perhaps a bit &#8216;hackish&#8217;, but it works quite well. I wanted to be able to index an array called obj in the following ways (example is with obj being 2D):</p>
<p>obj(1:2:end, 1:2:end)<br />
obj(1:2:end, 2:2:end)<br />
obj(2:2:end, 1:2:end)<br />
obj(2:2:end, 2:2:end)</p>
<p>For 3D it would be obj(1:2:end, 1:2:end, 1:2:end) etc. and there would be 2^3 combinations.</p>
<p>I used base-2 counting (via Matlab&#8217;s DEC2BIN) to enumerate the combinations and the actual indexing on obj was achieved by creating a string (e.g. &#8216;obj(1:2:end, 1:2:end)&#8217;) and then calling Matlab&#8217;s EVAL on that string.</p>
<p>The advantages of this method are: </p>
<p>one doesn&#8217;t have to learn a new syntax for indexing (I&#8217;ve been using Matlab for years and didn&#8217;t know of Loren&#8217;s cell array-based method, and it wouldn&#8217;t have occurred to me);</p>
<p>by generating strings, which are then evaluated, it&#8217;s easy to see exactly what would be computed (though actually the cell array method is equally transparent once you know about it).</p>
<p>I imagined the main disadvantage would be the performance hit of making Matlab to interpret code at run-time, however profiling on simple examples shows that there doesn&#8217;t appear to be a massive performance hit compared to some of the other Matlab functions I&#8217;m using (e.g. REGRESS, PADARRAY) in the code.</p>
<p>It would be interesting to know more about EVAL.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

