Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
a simple question

Subject: a simple question

From: Pinpress

Date: 16 May, 2011 14:47:04

Message: 1 of 4

Hi,

I am trying to programmable access a multi-dimensional matrix. Specifically, if X is of m-by-n-by-p (and so on), I would like to access the i_th dimension. If the 1st:

Y = X(1, :, :);

or the 3rd:

Y = X(:,:, 2);

The problem is that I need to use a variable to identify the i_th dimension. I wonder how to most efficiently/elegantly do this.

Thanks!

Subject: a simple question

From: TideMan

Date: 16 May, 2011 22:36:02

Message: 2 of 4

On May 17, 2:47 am, "Pinpress" <nospa...@yahoo.com> wrote:
> Hi,
>
> I am trying to programmable access a multi-dimensional matrix. Specifically, if X is of m-by-n-by-p (and so on), I would like to access the i_th dimension. If the 1st:
>
> Y = X(1, :, :);
>
> or the 3rd:
>
> Y = X(:,:, 2);
>
> The problem is that I need to use a variable to identify the i_th dimension. I wonder how to most efficiently/elegantly do this.
>
> Thanks!

This is a very interesting question.
It's a pity you used such a silly title. Otherwise, you might get
some interesting responses.
The only way I can think of is to use sub2ind, but it's not very
elegant..........

Subject: a simple question

From: Nasser M. Abbasi

Date: 16 May, 2011 23:12:08

Message: 3 of 4

On 5/16/2011 7:47 AM, Pinpress wrote:
> Hi,
>
> I am trying to programmable access a multi-dimensional matrix. Specifically,
> if X is of m-by-n-by-p (and so on), I would like to access the i_th dimension. If the 1st:
>
> Y = X(1, :, :);
>
> or the 3rd:
>
> Y = X(:,:, 2);
>
> The problem is that I need to use a variable to identify the i_th dimension.
> I wonder how to most efficiently/elegantly do this.
>
> Thanks!

You mean something like this?

--------------------
format short
A=rand(2,2,2)
r={':',':',':'};

dimension=1;
r{dimension}=dimension;
C=A(r{:})

dimension=2;
r{dimension}=dimension;
C=A(r{:})
----------------------

--Nasser

Subject: elegant way to access a variable dimension of matrix; Re: a simple question

From: Pinpress

Date: 17 May, 2011 03:07:03

Message: 4 of 4

Thanks for the reply. I think this is probably elegant enough, and it works!

I've also changed the subject, hopefully it can attract more eyeballs.


"Nasser M. Abbasi" <nma@12000.org> wrote in message <iqsb09$pfi$1@speranza.aioe.org>...
> On 5/16/2011 7:47 AM, Pinpress wrote:
> > Hi,
> >
> > I am trying to programmable access a multi-dimensional matrix. Specifically,
> > if X is of m-by-n-by-p (and so on), I would like to access the i_th dimension. If the 1st:
> >
> > Y = X(1, :, :);
> >
> > or the 3rd:
> >
> > Y = X(:,:, 2);
> >
> > The problem is that I need to use a variable to identify the i_th dimension.
> > I wonder how to most efficiently/elegantly do this.
> >
> > Thanks!
>
> You mean something like this?
>
> --------------------
> format short
> A=rand(2,2,2)
> r={':',':',':'};
>
> dimension=1;
> r{dimension}=dimension;
> C=A(r{:})
>
> dimension=2;
> r{dimension}=dimension;
> C=A(r{:})
> ----------------------
>
> --Nasser

Tags for this Thread

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.

Contact us