Effacer les filtres
Effacer les filtres

Fill array with elements located at specified subscripts in another array

2 vues (au cours des 30 derniers jours)
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?

Réponse acceptée

Matt J
Matt J le 17 Fév 2017
Ap=reshape( permute(A,[3,1,2]) , Asize(3),[] );
B=Ap(:, randperm(prod(Asize(1:2)),N) ).' ;
  1 commentaire
Aaron J. Hendrickson
Aaron J. Hendrickson le 17 Fév 2017
That works. One edit though: Just need to remove the .' in the second line. Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by