Got Questions? Get Answers.
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:
how to use a vector as an index for a matrix

Subject: how to use a vector as an index for a matrix

From: mono

Date: 9 Aug, 2012 13:13:16

Message: 1 of 12

Hello all,

I want to use a vector as index for a matrix. For example, t=[2,3], I want to point to the value in the matrix M at M(2,3). If I command as M(t), actually it points to M(2) and M(3). How can I use t to point to M(2,3)?

Thanks in advance.

mono

Subject: how to use a vector as an index for a matrix

From: dpb

Date: 9 Aug, 2012 13:28:08

Message: 2 of 12

On 8/9/2012 8:13 AM, mono wrote:
> Hello all,
>
> I want to use a vector as index for a matrix. For example, t=[2,3], I
> want to point to the value in the matrix M at M(2,3). If I command as
> M(t), actually it points to M(2) and M(3). How can I use t to point to
> M(2,3)?

doc sub2ind % and friends

--

Subject: how to use a vector as an index for a matrix

From: Justin Ashmall

Date: 9 Aug, 2012 13:33:19

Message: 3 of 12

> I want to use a vector as index for a matrix. For example, t=[2,3], I want to point to the value in the matrix M at M(2,3). If I command as M(t), actually it points to M(2) and M(3). How can I use t to point to M(2,3)?

You want the sub2ind function to convert to a linear index.

In your example, this would do the trick:

 M( sub2ind(size(M), 2, 3) )

You could replace the 2 and 3 with (equal sized) vectors to get multiple values back.

Justin

Subject: how to use a vector as an index for a matrix

From: mono

Date: 9 Aug, 2012 14:00:23

Message: 4 of 12

"Justin Ashmall" <juestan-matlabnews6h8@yahoo.co.uk> wrote in message <k00e6v$4fi$1@newscl01ah.mathworks.com>...
> > I want to use a vector as index for a matrix. For example, t=[2,3], I want to point to the value in the matrix M at M(2,3). If I command as M(t), actually it points to M(2) and M(3). How can I use t to point to M(2,3)?
>
> You want the sub2ind function to convert to a linear index.
>
> In your example, this would do the trick:
>
> M( sub2ind(size(M), 2, 3) )
>
> You could replace the 2 and 3 with (equal sized) vectors to get multiple values back.
>
> Justin

Hi Justin,

Thanks for your reply. But I don't think it solves my problem. The thing is t is a vector, and its size is not fixed. It makes trouble to use its value inside. t can be t=[2,3], can also be t=[2,3,4]. Of course when t=[2,3], the matrix M is 2 dimensional, and when t=[2,3,4], M is 3 dimensional. You suggested to use sub2ind, however it still needs t as put. Obviously, M(sub2ind(size(M),t)) is not working. Any further suggestion?

Thanks.

mono

Subject: how to use a vector as an index for a matrix

From: dpb

Date: 9 Aug, 2012 14:23:11

Message: 5 of 12

On 8/9/2012 9:00 AM, mono wrote:
> "Justin Ashmall" <juestan-matlabnews6h8@yahoo.co.uk> wrote in message
> <k00e6v$4fi$1@newscl01ah.mathworks.com>...
>> > I want to use a vector as index for a matrix. For example, t=[2,3],
>> I want to point to the value in the matrix M at M(2,3). If I command
>> as M(t), actually it points to M(2) and M(3). How can I use t to point
>> to M(2,3)?
>>
>> You want the sub2ind function to convert to a linear index.
...

> ... You suggested to use sub2ind, however it still needs t as
> put. Obviously, M(sub2ind(size(M),t)) is not working. Any further
> suggestion?

M(sub2ind(size(M),num2cell(t){:}))

You've got to turn the vector to a comma list somehow since sub2ind
doesn't understand cells and vectors are, well, vectors, not multiple
arguments...

My release predates the multilevel indexing so I'm presuming the above
syntax actually does work w/o an intermediary temporary variable.

--

Subject: how to use a vector as an index for a matrix

From: Justin Ashmall

Date: 9 Aug, 2012 14:27:16

Message: 6 of 12

> Thanks for your reply. But I don't think it solves my problem. The thing is t is a vector, and its size is not fixed. It makes trouble to use its value inside. t can be t=[2,3], can also be t=[2,3,4]. Of course when t=[2,3], the matrix M is 2 dimensional, and when t=[2,3,4], M is 3 dimensional. You suggested to use sub2ind, however it still needs t as put. Obviously, M(sub2ind(size(M),t)) is not working. Any further suggestion?


Why not something simple like this with if-else or case-switch?

if ndims(t)==2
    lind = sub2ind(size(M), t(1),t(2));
else if ndims(t)==3
    lind = sub2ind(size(M), t(1),t(2),t(3));
...

If you really want a one for all situation AND your t is always a vector (i.e. points to a single value in M) you could do it this way using a cell array in place of a variable number of inputs:

>> M=rand(3,4,5);
>> t = [2,3,4];
>> ct = num2cell(t);
>> M( sub2ind(size(M), ct{:}) )
ans = 0.9926
>> M( sub2ind(size(M), 2,3,4) )
ans = 0.9926

To see how this works lookup "comma-separated list" in the documentation.

Subject: how to use a vector as an index for a matrix

From: mono

Date: 9 Aug, 2012 14:35:13

Message: 7 of 12

"Justin Ashmall" <juestan-matlabnews6h8@yahoo.co.uk> wrote in message <k00hc4$frc$1@newscl01ah.mathworks.com>...
> > Thanks for your reply. But I don't think it solves my problem. The thing is t is a vector, and its size is not fixed. It makes trouble to use its value inside. t can be t=[2,3], can also be t=[2,3,4]. Of course when t=[2,3], the matrix M is 2 dimensional, and when t=[2,3,4], M is 3 dimensional. You suggested to use sub2ind, however it still needs t as put. Obviously, M(sub2ind(size(M),t)) is not working. Any further suggestion?
>
>
> Why not something simple like this with if-else or case-switch?
>
> if ndims(t)==2
> lind = sub2ind(size(M), t(1),t(2));
> else if ndims(t)==3
> lind = sub2ind(size(M), t(1),t(2),t(3));
> ...
>
> If you really want a one for all situation AND your t is always a vector (i.e. points to a single value in M) you could do it this way using a cell array in place of a variable number of inputs:
>
> >> M=rand(3,4,5);
> >> t = [2,3,4];
> >> ct = num2cell(t);
> >> M( sub2ind(size(M), ct{:}) )
> ans = 0.9926
> >> M( sub2ind(size(M), 2,3,4) )
> ans = 0.9926
>
> To see how this works lookup "comma-separated list" in the documentation.

wow, this is really smart to use cell. Good idea. I think my problem solved. Thanks a lot.

mono

Subject: how to use a vector as an index for a matrix

From: Matt J

Date: 9 Aug, 2012 14:39:14

Message: 8 of 12

"mono" wrote in message <k00d1c$13u$1@newscl01ah.mathworks.com>...
> Hello all,
>
> I want to use a vector as index for a matrix. For example, t=[2,3], I want to point to the value in the matrix M at M(2,3). If I command as M(t), actually it points to M(2) and M(3). How can I use t to point to M(2,3)?
============

I may be missing something, but I don't see why people are recommending sub2ind here. This should do it:

tcell=num2cell(t);
M(tcell{:})

Subject: how to use a vector as an index for a matrix

From: Bruno Luong

Date: 9 Aug, 2012 14:42:15

Message: 9 of 12

"mono" wrote in message <k00fpn$94r$1@newscl01ah.mathworks.com>...
> "Justin Ashmall" <juestan-matlabnews6h8@yahoo.co.uk> wrote in message <k00e6v$4fi$1@newscl01ah.mathworks.com>...
> > > I want to use a vector as index for a matrix. For example, t=[2,3], I want to point to the value in the matrix M at M(2,3). If I command as M(t), actually it points to M(2) and M(3). How can I use t to point to M(2,3)?
> >
> > You want the sub2ind function to convert to a linear index.
> >
> > In your example, this would do the trick:
> >
> > M( sub2ind(size(M), 2, 3) )
> >
> > You could replace the 2 and 3 with (equal sized) vectors to get multiple values back.
> >
> > Justin
>
> Hi Justin,
>
> Thanks for your reply. But I don't think it solves my problem. The thing is t is a vector, and its size is not fixed. It makes trouble to use its value inside. t can be t=[2,3], can also be t=[2,3,4]. Of course when t=[2,3], the matrix M is 2 dimensional, and when t=[2,3,4], M is 3 dimensional. You suggested to use sub2ind, however it still needs t as put. Obviously, M(sub2ind(size(M),t)) is not working. Any further suggestion?

sub2indvec = @(s,t) 1 + [1 cumprod(s(1:end-1))]*(t(:)-1);
M(sub2indvec(size(M),t))

% Bruno

Subject: how to use a vector as an index for a matrix

From: dpb

Date: 9 Aug, 2012 14:45:05

Message: 10 of 12

On 8/9/2012 9:39 AM, Matt J wrote:
...

> I may be missing something, but I don't see why people are recommending
> sub2ind here. This should do it:
>
> tcell=num2cell(t);
> M(tcell{:})

Actually, you're correct, Matt. :)

Classic case of misdirection where the vector gets one thinking in the
wrong direction...

I'm curious since my version doesn't have the multilevel indexing--does

M(num2cell(t){:});

work w/o the temporary (I'd hope)?

--

Subject: how to use a vector as an index for a matrix

From: Bruno Luong

Date: 9 Aug, 2012 14:51:16

Message: 11 of 12

dpb <none@non.net> wrote in message <k00idl$ln6$1@speranza.aioe.org>...

> I'm curious since my version doesn't have the multilevel indexing--does
>
> M(num2cell(t){:});
>
> work w/o the temporary (I'd hope)?

No. Discussed billion of times.

Bruno

Subject: how to use a vector as an index for a matrix

From: Justin Ashmall

Date: 9 Aug, 2012 14:54:14

Message: 12 of 12

"Matt J" wrote
> I may be missing something, but I don't see why people are recommending sub2ind here. This should do it:
> tcell=num2cell(t);
> M(tcell{:})

That's a fair point Matt, esp since we've strayed into comma-separated lists here.
I'd still rather use sub2ind without comma-lists myself...

Tags for this Thread

No tags are associated with 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