Thread Subject: index array with variable number of dimensions

Subject: index array with variable number of dimensions

From: scott

Date: 10 Nov, 2008 19:58:02

Message: 1 of 7

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.

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.

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.

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.

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)).

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])??

(note that the last thing doesn't work, but it gives the idea of what I'd like)

Thanks much.

Subject: index array with variable number of dimensions

From: Loren Shure

Date: 10 Nov, 2008 20:32:18

Message: 2 of 7

In article <gfa3oa$ej8$1@fred.mathworks.com>,
grenblau@yahoo.deletethis.com says...
> 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.
>
> 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.
>
> 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.
>
> 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.
>
> 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)).
>
> 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])??
>
> (note that the last thing doesn't work, but it gives the idea of what I'd like)
>
> Thanks much.
>

nd = ndims(x);
index = repmat({':'},1,nd);

x(index{:}) gives you nd indexing. Now just replace whichever dimension
you want in the vector index and you should be all set.

--
Loren
http://blogs.mathworks.com/loren

Subject: index array with variable number of dimensions

From: Walter Roberson

Date: 10 Nov, 2008 20:34:19

Message: 3 of 7

scott wrote:

> 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])??

Use a cell array.

TheseIndices = {2 3 4};
statisticArray(:,TheseIndices{:});

(And if you prefer to work with an array of indices, remember num2cell can
transform an array of numbers into a cell array of individual numbers.)


--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: index array with variable number of dimensions

From: scott

Date: 10 Nov, 2008 21:54:02

Message: 4 of 7

Walter Roberson <roberson@hushmail.com> wrote in message <YU0Sk.2513$lo6.2424@newsfe01.iad>...
> scott wrote:
>
> > 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])??
>
> Use a cell array.
>
> TheseIndices = {2 3 4};
> statisticArray(:,TheseIndices{:});
>
> (And if you prefer to work with an array of indices, remember num2cell can
> transform an array of numbers into a cell array of individual numbers.)
>
>
> --


That seems to have done it - thanks Walter and Loren

Subject: index array with variable number of dimensions

From: Walter Roberson

Date: 10 Nov, 2008 22:46:09

Message: 5 of 7

Loren Shure wrote:
 
> nd = ndims(x);
> index = repmat({':'},1,nd);
 
> x(index{:}) gives you nd indexing.

Interesting -- you can't use 'end' though, and you have to use numeric cell
arrays instead of strings of numbers... e.g.,

index = {':' 9 ':'};
x(index{:})

-not-

index = {':' '9' ':'};
x(index{:})

The ':' must be treated as a special case ?

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: index array with variable number of dimensions

From: Loren Shure

Date: 11 Nov, 2008 14:00:52

Message: 6 of 7

In article <yQ2Sk.3011$Gm3.2484@newsfe01.iad>, roberson@hushmail.com
says...
> Loren Shure wrote:
>
> > nd = ndims(x);
> > index = repmat({':'},1,nd);
>
> > x(index{:}) gives you nd indexing.
>
> Interesting -- you can't use 'end' though, and you have to use numeric cell
> arrays instead of strings of numbers... e.g.,
>
> index = {':' 9 ':'};
> x(index{:})
>
> -not-
>
> index = {':' '9' ':'};
> x(index{:})
>
> The ':' must be treated as a special case ?
>
>


yes, and if you create and use the cell area inside an indexed
expression, then you can use end, (not 'end').

x(max(10,end),1)
--
Loren
http://blogs.mathworks.com/loren

Subject: index array with variable number of dimensions

From: Walter Roberson

Date: 10 Nov, 2008 20:15:50

Message: 7 of 7

scott wrote:
 
> 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])??

Use a cell array.

TheseIndices = {2 3 4};
statisticArray(:,TheseIndices{:});

(And if you prefer to work with an array of indices, remember num2cell can
transform an array of numbers into a cell array of individual numbers.)


--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
array scott 10 Nov, 2008 15:00:22
multidimensiona... scott 10 Nov, 2008 15:00:22
indexing scott 10 Nov, 2008 15:00:22
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com