<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239004</link>
    <title>MATLAB Central Newsreader - index array with variable number of dimensions</title>
    <description>Feed for thread: index array with variable number of dimensions</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 10 Nov 2008 19:58:02 -0500</pubDate>
      <title>index array with variable number of dimensions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239004#610137</link>
      <author>scott </author>
      <description>I have a function where I allocate a multidimensional numeric array.  The array dimensionality depends upon arguments passed to the function.  I'd like to know if there is a way to flexibly index the array.&lt;br&gt;
&lt;br&gt;
For example, let's say I have a time series of 100 timepoints repeated 30 times.  I want to vary two different parameters across 4 and 5 values and calculate some statistic over the repeats for each parameter combination and timepoint. &lt;br&gt;
&lt;br&gt;
So my data could be a big array of size 100 x 30 x 4 x 5.  And my statistic array would be an array of size 100 x 4 x 5.&lt;br&gt;
&lt;br&gt;
That's no problem.  But what if I want to make my function flexible so that if I wanted to vary 3 parameters instead of 2 I could do it with the same code.  So let's say I have another parameter that I vary over 6 values.  Then my data array would be size 100 x 30 x 4 x 5 x 6, and my statistic array would be size 100 x 4 x 5 x 6.&lt;br&gt;
&lt;br&gt;
My problem is that I don't see any way to do the indexing.  If I want to assign a value to the statistic array, I can do it if I know how many dimensions there are:  statisticArray(:,2,3) = mean(dataArray(:,:,2,3),2)  but if there is sometimes an extra dimension then it would have to be statisticArray(:,2,3,4)=mean(dataArray(:,:,2,3,4)).  &lt;br&gt;
&lt;br&gt;
Is there any way to have a variable number of dimensions and, say, put the indexes I want for these into a 1D vector and then just index something like:  statisticArray(:,[2 3 4])??&lt;br&gt;
&lt;br&gt;
(note that the last thing doesn't work, but it gives the idea of what I'd like)&lt;br&gt;
&lt;br&gt;
Thanks much.</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 20:32:18 -0500</pubDate>
      <title>Re: index array with variable number of dimensions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239004#610147</link>
      <author>Loren Shure</author>
      <description>In article &amp;lt;gfa3oa$ej8$1@fred.mathworks.com&amp;gt;, &lt;br&gt;
grenblau@yahoo.deletethis.com says...&lt;br&gt;
&amp;gt; I have a function where I allocate a multidimensional numeric array.  The array dimensionality depends upon arguments passed to the function.  I'd like to know if there is a way to flexibly index the array.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; For example, let's say I have a time series of 100 timepoints repeated 30 times.  I want to vary two different parameters across 4 and 5 values and calculate some statistic over the repeats for each parameter combination and timepoint. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So my data could be a big array of size 100 x 30 x 4 x 5.  And my statistic array would be an array of size 100 x 4 x 5.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; That's no problem.  But what if I want to make my function flexible so that if I wanted to vary 3 parameters instead of 2 I could do it with the same code.  So let's say I have another parameter that I vary over 6 values.  Then my data array would be size 100 x 30 x 4 x 5 x 6, and my statistic array would be size 100 x 4 x 5 x 6.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My problem is that I don't see any way to do the indexing.  If I want to assign a value to the statistic array, I can do it if I know how many dimensions there are:  statisticArray(:,2,3) = mean(dataArray(:,:,2,3),2)  but if there is sometimes an extra dimension then it would have to be statisticArray(:,2,3,4)=mean(dataArray(:,:,2,3,4)).  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Is there any way to have a variable number of dimensions and, say, put the indexes I want for these into a 1D vector and then just index something like:  statisticArray(:,[2 3 4])??&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; (note that the last thing doesn't work, but it gives the idea of what I'd like)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks much.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
nd = ndims(x);&lt;br&gt;
index = repmat({':'},1,nd);&lt;br&gt;
&lt;br&gt;
x(index{:}) gives you nd indexing.  Now just replace whichever dimension &lt;br&gt;
you want in the vector index and you should be all set.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Loren&lt;br&gt;
&lt;a href=&quot;http://blogs.mathworks.com/loren&quot;&gt;http://blogs.mathworks.com/loren&lt;/a&gt;</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 20:34:19 -0500</pubDate>
      <title>Re: index array with variable number of dimensions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239004#610149</link>
      <author>Walter Roberson</author>
      <description>scott wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; Is there any way to have a variable number of dimensions and, say, put the indexes&lt;br&gt;
&amp;gt; I want for these into a 1D vector and then just index something like:&lt;br&gt;
&amp;gt;  statisticArray(:,[2 3 4])??&lt;br&gt;
&lt;br&gt;
Use a cell array.&lt;br&gt;
&lt;br&gt;
TheseIndices = {2 3 4};&lt;br&gt;
statisticArray(:,TheseIndices{:});&lt;br&gt;
&lt;br&gt;
(And if you prefer to work with an array of indices, remember num2cell can&lt;br&gt;
transform an array of numbers into a cell array of individual numbers.)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
.signature note: I am now avoiding replying to unclear or ambiguous postings.&lt;br&gt;
Please review questions before posting them. Be specific. Use examples of what you mean,&lt;br&gt;
of what you don't mean. Specify boundary conditions, and data classes and value&lt;br&gt;
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 21:54:02 -0500</pubDate>
      <title>Re: index array with variable number of dimensions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239004#610167</link>
      <author>scott </author>
      <description>Walter Roberson &amp;lt;roberson@hushmail.com&amp;gt; wrote in message &amp;lt;YU0Sk.2513$lo6.2424@newsfe01.iad&amp;gt;...&lt;br&gt;
&amp;gt; scott wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Is there any way to have a variable number of dimensions and, say, put the indexes&lt;br&gt;
&amp;gt; &amp;gt; I want for these into a 1D vector and then just index something like:&lt;br&gt;
&amp;gt; &amp;gt;  statisticArray(:,[2 3 4])??&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Use a cell array.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; TheseIndices = {2 3 4};&lt;br&gt;
&amp;gt; statisticArray(:,TheseIndices{:});&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; (And if you prefer to work with an array of indices, remember num2cell can&lt;br&gt;
&amp;gt; transform an array of numbers into a cell array of individual numbers.)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -- &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
That seems to have done it - thanks Walter and Loren</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 22:46:09 -0500</pubDate>
      <title>Re: index array with variable number of dimensions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239004#610178</link>
      <author>Walter Roberson</author>
      <description>Loren Shure wrote:&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; nd = ndims(x);&lt;br&gt;
&amp;gt; index = repmat({':'},1,nd);&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; x(index{:}) gives you nd indexing.&lt;br&gt;
&lt;br&gt;
Interesting -- you can't use 'end' though, and you have to use numeric cell&lt;br&gt;
arrays instead of strings of numbers... e.g., &lt;br&gt;
&lt;br&gt;
index = {':' 9 ':'};&lt;br&gt;
x(index{:})&lt;br&gt;
&lt;br&gt;
-not-&lt;br&gt;
&lt;br&gt;
index = {':' '9' ':'};&lt;br&gt;
x(index{:})&lt;br&gt;
&lt;br&gt;
The ':' must be treated as a special case ?&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
.signature note: I am now avoiding replying to unclear or ambiguous postings.&lt;br&gt;
Please review questions before posting them. Be specific. Use examples of what you mean,&lt;br&gt;
of what you don't mean. Specify boundary conditions, and data classes and value&lt;br&gt;
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?</description>
    </item>
    <item>
      <pubDate>Tue, 11 Nov 2008 14:00:52 -0500</pubDate>
      <title>Re: index array with variable number of dimensions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239004#610279</link>
      <author>Loren Shure</author>
      <description>In article &amp;lt;yQ2Sk.3011$Gm3.2484@newsfe01.iad&amp;gt;, roberson@hushmail.com &lt;br&gt;
says...&lt;br&gt;
&amp;gt; Loren Shure wrote:&lt;br&gt;
&amp;gt;  &lt;br&gt;
&amp;gt; &amp;gt; nd = ndims(x);&lt;br&gt;
&amp;gt; &amp;gt; index = repmat({':'},1,nd);&lt;br&gt;
&amp;gt;  &lt;br&gt;
&amp;gt; &amp;gt; x(index{:}) gives you nd indexing.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Interesting -- you can't use 'end' though, and you have to use numeric cell&lt;br&gt;
&amp;gt; arrays instead of strings of numbers... e.g., &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; index = {':' 9 ':'};&lt;br&gt;
&amp;gt; x(index{:})&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -not-&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; index = {':' '9' ':'};&lt;br&gt;
&amp;gt; x(index{:})&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The ':' must be treated as a special case ?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
yes, and if you create and use the cell area inside an indexed &lt;br&gt;
expression, then you can use end, (not 'end').&lt;br&gt;
&lt;br&gt;
x(max(10,end),1)&lt;br&gt;
-- &lt;br&gt;
Loren&lt;br&gt;
&lt;a href=&quot;http://blogs.mathworks.com/loren&quot;&gt;http://blogs.mathworks.com/loren&lt;/a&gt;</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 20:15:50 -0500</pubDate>
      <title>Re: index array with variable number of dimensions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239004#610503</link>
      <author>Walter Roberson</author>
      <description>scott wrote:&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; Is there any way to have a variable number of dimensions and, say, put the indexes&lt;br&gt;
&amp;gt; I want for these into a 1D vector and then just index something like:&lt;br&gt;
&amp;gt;  statisticArray(:,[2 3 4])??&lt;br&gt;
&lt;br&gt;
Use a cell array.&lt;br&gt;
&lt;br&gt;
TheseIndices = {2 3 4};&lt;br&gt;
statisticArray(:,TheseIndices{:});&lt;br&gt;
&lt;br&gt;
(And if you prefer to work with an array of indices, remember num2cell can&lt;br&gt;
transform an array of numbers into a cell array of individual numbers.)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
.signature note: I am now avoiding replying to unclear or ambiguous postings.&lt;br&gt;
Please review questions before posting them. Be specific. Use examples of what you mean,&lt;br&gt;
of what you don't mean. Specify boundary conditions, and data classes and value&lt;br&gt;
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?</description>
    </item>
  </channel>
</rss>

