MATLAB: Extract rows in 3D matrix according to indexes

3dcolumnsextractindexing

x2 = zeros(60, 46000, 'single');
for N = 1:46000
x2(:,N) = x(:,i(N),N);
end
Hi. I have a 3D matrix x with the dimensions 60x100x40000 and a 1×46000 vector i with an index for each matrix in the 3rd dimension telling me which columns I need to extract (The values for i are between 1 & 100). So ultimately, I would like to end up with a 60×40000 matrix, using i to extract from x.
My solution so far can be seen above. It works, but takes forever (especially since I have to repeat the process almost 100 times. Does anyone know of a direct way I can do this?
Thanks!

Best Answer

% Let X - your data with size (60x100x40000)
% ii - your vector with index of columns X from each frame of X (1x46000)
[m,n,k] = size(X);
x1 = reshape(X,m,[]);
x2 = x1(:,n*(0:k-1)+ii(1:k));