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 two vectors as ordered pairs?

Subject: How to use two vectors as ordered pairs?

From: Paulo Guimaraes

Date: 6 Apr, 2012 22:02:17

Message: 1 of 3

Hi,

I have the following problem and would appreciate if someone can help me.

r = a square matrix.
v= is another matrix with n rows and two columns.
k=the outcome, a vector.


I would like to use each row of v as indices for the get values in the matrix r and save them in a new vector k. For example:

r=[0 0 1 5;4 0 2 5;3 3 3 5;2 2 2 2]
v=[1 2; 2 3; 4 1;3 3];

k should be:

k=[0 2 2 3]

Some one can help me?

Thanks

P.

Subject: How to use two vectors as ordered pairs?

From: Roger Stafford

Date: 6 Apr, 2012 22:21:12

Message: 2 of 3

"Paulo Guimaraes" wrote in message <jlnp58$fbs$1@newscl01ah.mathworks.com>...
> I would like to use each row of v as indices for the get values in the matrix r and save them in a new vector k. For example:
>
> r=[0 0 1 5;4 0 2 5;3 3 3 5;2 2 2 2]
> v=[1 2; 2 3; 4 1;3 3];
>
> k should be:
>
> k=[0 2 2 3]
- - - - - - - -
 k = r(v(:,1)+size(r,1)*(v(:,2)-1)); % Or use 'subs2ind'

Roger Stafford

Subject: How to use two vectors as ordered pairs?

From: Paulo Guimaraes

Date: 6 Apr, 2012 22:42:38

Message: 3 of 3

"Roger Stafford" wrote in message <jlnq8o$j8m$1@newscl01ah.mathworks.com>...
> "Paulo Guimaraes" wrote in message <jlnp58$fbs$1@newscl01ah.mathworks.com>...
> > I would like to use each row of v as indices for the get values in the matrix r and save them in a new vector k. For example:
> >
> > r=[0 0 1 5;4 0 2 5;3 3 3 5;2 2 2 2]
> > v=[1 2; 2 3; 4 1;3 3];
> >
> > k should be:
> >
> > k=[0 2 2 3]
> - - - - - - - -
> k = r(v(:,1)+size(r,1)*(v(:,2)-1)); % Or use 'subs2ind'
>
> Roger Stafford

Very nice! Thanks!

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