Extract rows in 3D matrix according to indexes

11 ビュー (過去 30 日間)
Marc Jakobi
Marc Jakobi 2015 年 9 月 28 日
編集済み: Andrei Bobrov 2015 年 9 月 29 日
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!

採用された回答

Andrei Bobrov
Andrei Bobrov 2015 年 9 月 29 日
編集済み: Andrei Bobrov 2015 年 9 月 29 日
% 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));

その他の回答 (1 件)

Stephen23
Stephen23 2015 年 9 月 28 日
編集済み: Stephen23 2015 年 9 月 29 日
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.

カテゴリ

Help Center および File ExchangeFinancial Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by