http://www.mathworks.com/matlabcentral/newsreader/view_thread/329030
MATLAB Central Newsreader  Extract column from matrix several times
Feed for thread: Extract column from matrix several times
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 15 May 2013 23:11:28 +0000
Extract column from matrix several times
http://www.mathworks.com/matlabcentral/newsreader/view_thread/329030#904513
Tintin Milou
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:<br>
<br>
B = [A(:,x1,y1); A(:,x2,y2); ...];<br>
<br>
Since x and y are potentially fairly long vectors, I'm looking for a fast solution that avoids a forloop. <br>
<br>
Thanks a lot!

Thu, 16 May 2013 00:30:11 +0000
Re: Extract column from matrix several times
http://www.mathworks.com/matlabcentral/newsreader/view_thread/329030#904514
James Tursa
"Tintin Milou" wrote in message <kn14mv$ana$1@newscl01ah.mathworks.com>...<br>
> 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:<br>
> <br>
> B = [A(:,x1,y1); A(:,x2,y2); ...];<br>
> <br>
> Since x and y are potentially fairly long vectors, I'm looking for a fast solution that avoids a forloop. <br>
<br>
Something like this perhaps (caution: untested as I am not on a machine with MATLAB at the moment):<br>
<br>
z = x + (y1) * size(A,2); % Generate equivalent 2D indexes<br>
B = A(:,z); % Index into A as a 2D array<br>
<br>
James Tursa<br>

Thu, 16 May 2013 14:48:09 +0000
Re: Extract column from matrix several times
http://www.mathworks.com/matlabcentral/newsreader/view_thread/329030#904544
James Tursa
"James Tursa" wrote in message <kn19aj$m2a$1@newscl01ah.mathworks.com>...<br>
> "Tintin Milou" wrote in message <kn14mv$ana$1@newscl01ah.mathworks.com>...<br>
> > 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:<br>
> > <br>
> > B = [A(:,x1,y1); A(:,x2,y2); ...];<br>
> > <br>
> > Since x and y are potentially fairly long vectors, I'm looking for a fast solution that avoids a forloop. <br>
> <br>
> Something like this perhaps (caution: untested as I am not on a machine with MATLAB at the moment):<br>
> <br>
> z = x + (y1) * size(A,2); % Generate equivalent 2D indexes<br>
> B = A(:,z); % Index into A as a 2D array<br>
<br>
And if you want a single column result you can add:<br>
<br>
B = B(:);<br>
<br>
James Tursa