フィルターのクリア

Fill array with elements located at specified subscripts in another array

2 ビュー (過去 30 日間)
I have a 3D array, A, of size IxJxK as well as a list of N 2D coordinates in an array I (see below). I want to fill a 2D array, B, of size KxN with the elements of A as indicated in the comments in the code below.
Asize = [128,128,64];
A = rand(Asize(1),Asize(2),Asize(3));
N = 1e+3;
[Ir,Ic] = ind2sub(Asize,randperm(prod(Asize(1:2)),N));
I = [Ir',Ic'];
B = zeros(Asize(3),N);
% B(:,n) should contain the elements A(I(n,1),I(n,2),:)
Is there a fast way to do this that does not involve loops?

採用された回答

Matt J
Matt J 2017 年 2 月 17 日
Ap=reshape( permute(A,[3,1,2]) , Asize(3),[] );
B=Ap(:, randperm(prod(Asize(1:2)),N) ).' ;
  1 件のコメント
Aaron J. Hendrickson
Aaron J. Hendrickson 2017 年 2 月 17 日
That works. One edit though: Just need to remove the .' in the second line. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by