필터 지우기
필터 지우기

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) ).' ;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by