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 get matrix elements...?

Subject: how to get matrix elements...?

From: stani S.

Date: 19 Oct, 2010 20:08:04

Message: 1 of 5

Hi

Suppose I have a matrix a = [ 2 3 4; 5 6 7]
and a vector b =[ 2 1 2]

What is the fastest way to obtain elements of the matrix a, where b is the the a's row, i. e.
a(b(1), 1) = 5
a(b(2), 2) = 3
a(b(3), 3) = 7

Thanks!

Subject: how to get matrix elements...?

From: Sean

Date: 19 Oct, 2010 20:20:31

Message: 2 of 5

"stani S." <stanci10@hotmail.com> wrote in message <i9ktr4$dl4$1@fred.mathworks.com>...
> Hi
>
> Suppose I have a matrix a = [ 2 3 4; 5 6 7]
> and a vector b =[ 2 1 2]
>
> What is the fastest way to obtain elements of the matrix a, where b is the the a's row, i. e.
> a(b(1), 1) = 5
> a(b(2), 2) = 3
> a(b(3), 3) = 7
>
> Thanks!

if you want a row vector:
a(sub2ind(size(a),b,1:3))

if you want a column vector, transpose that. (i.e. add .' to the end of the line)

Subject: how to get matrix elements...?

From: Sean

Date: 19 Oct, 2010 20:22:04

Message: 3 of 5

"stani S." <stanci10@hotmail.com> wrote in message <i9ktr4$dl4$1@fred.mathworks.com>...
> Hi
>
> Suppose I have a matrix a = [ 2 3 4; 5 6 7]
> and a vector b =[ 2 1 2]
>
> What is the fastest way to obtain elements of the matrix a, where b is the the a's row, i. e.
> a(b(1), 1) = 5
> a(b(2), 2) = 3
> a(b(3), 3) = 7
>
> Thanks!

if you want a row vector:
a(sub2ind(size(a),b,1:3))

if you want a column vector, transpose that. (i.e. add .' to the end of the line)

Subject: how to get matrix elements...?

From: dpb

Date: 19 Oct, 2010 20:17:58

Message: 4 of 5

stani S. wrote:
> Hi
> Suppose I have a matrix a = [ 2 3 4; 5 6 7]
> and a vector b =[ 2 1 2]
>
> What is the fastest way to obtain elements of the matrix a, where b is
> the the a's row, i. e.
> a(b(1), 1) = 5
> a(b(2), 2) = 3
> a(b(3), 3) = 7

That's about as good as any; otoh, could do sotoo

 >> a(sub2ind(a,b,1:3))
ans =
      5 3 7
 >>

altho not sure it's any "better"...

--

Subject: how to get matrix elements...?

From: stani S.

Date: 19 Oct, 2010 20:30:09

Message: 5 of 5

Great! Thanks!
dpb <none@non.net> wrote in message <i9kum4$107$1@news.eternal-september.org>...
> stani S. wrote:
> > Hi
> > Suppose I have a matrix a = [ 2 3 4; 5 6 7]
> > and a vector b =[ 2 1 2]
> >
> > What is the fastest way to obtain elements of the matrix a, where b is
> > the the a's row, i. e.
> > a(b(1), 1) = 5
> > a(b(2), 2) = 3
> > a(b(3), 3) = 7
>
> That's about as good as any; otoh, could do sotoo
>
> >> a(sub2ind(a,b,1:3))
> ans =
> 5 3 7
> >>
>
> altho not sure it's any "better"...
>
> --

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