Extract rows in 3D matrix according to indexes
11 views (last 30 days)
Show older comments
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 1x46000 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 60x40000 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!
0 Comments
Accepted Answer
Andrei Bobrov
on 29 Sep 2015
Edited: Andrei Bobrov
on 29 Sep 2015
% 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));
0 Comments
More Answers (1)
Stephen23
on 28 Sep 2015
Edited: Stephen23
on 29 Sep 2015
Perhaps you could try the values of the index vector directly:
% index:
idx = [2,3,1,2];
% fake data:
X = reshape(1:2*3*4,2,3,4);
% extract columns:
Y = X(:,idx,:);
% create index for pages:
[R,C,~] = size(Y);
idy = cumsum([1:R;R*(C+1)*ones(C-1,2)]);
% extract pages:
A = Y(idy).';
This gives the identical results to your loop, but with the advantange that it does not use any loop.
0 Comments
See Also
Categories
Find more on Financial Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!