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:
Extract column from matrix several times

Subject: Extract column from matrix several times

From: Tintin Milou

Date: 15 May, 2013 23:11:28

Message: 1 of 3

I have vectors x and y, which contain indexes. I further have a 3D matrix A. Now, I'd like to set up the following matrix:

B = [A(:,x1,y1); A(:,x2,y2); ...];

Since x and y are potentially fairly long vectors, I'm looking for a fast solution that avoids a for-loop.

Thanks a lot!

Subject: Extract column from matrix several times

From: James Tursa

Date: 16 May, 2013 00:30:11

Message: 2 of 3

"Tintin Milou" wrote in message <kn14mv$ana$1@newscl01ah.mathworks.com>...
> I have vectors x and y, which contain indexes. I further have a 3D matrix A. Now, I'd like to set up the following matrix:
>
> B = [A(:,x1,y1); A(:,x2,y2); ...];
>
> Since x and y are potentially fairly long vectors, I'm looking for a fast solution that avoids a for-loop.

Something like this perhaps (caution: untested as I am not on a machine with MATLAB at the moment):

z = x + (y-1) * size(A,2); % Generate equivalent 2D indexes
B = A(:,z); % Index into A as a 2D array

James Tursa
 

Subject: Extract column from matrix several times

From: James Tursa

Date: 16 May, 2013 14:48:09

Message: 3 of 3

"James Tursa" wrote in message <kn19aj$m2a$1@newscl01ah.mathworks.com>...
> "Tintin Milou" wrote in message <kn14mv$ana$1@newscl01ah.mathworks.com>...
> > I have vectors x and y, which contain indexes. I further have a 3D matrix A. Now, I'd like to set up the following matrix:
> >
> > B = [A(:,x1,y1); A(:,x2,y2); ...];
> >
> > Since x and y are potentially fairly long vectors, I'm looking for a fast solution that avoids a for-loop.
>
> Something like this perhaps (caution: untested as I am not on a machine with MATLAB at the moment):
>
> z = x + (y-1) * size(A,2); % Generate equivalent 2D indexes
> B = A(:,z); % Index into A as a 2D array

And if you want a single column result you can add:

B = B(:);

James Tursa

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