필터 지우기
필터 지우기

I have data in 3D array and I know indices in the first two dimensions. How do I collect all data without for loop?

조회 수: 1 (최근 30일)
For example
Data(:,:,1) = [1,2,3
4,5,6
7,8,9];
Data(:,:,2) = [11,22,33
44,55,66
77,88,99];
I expect the output to be output(:,:,1) = [1,3
7,5]
output(:,:,2) = [11,33
77,55]
I know that the index are idxRow = [1,1
3,2];
idxCol = [1,3
1,2];
How do I use idxRow and idxCol to extract everything from Data?

답변 (1개)

Matt J
Matt J 2023년 5월 18일
편집: Matt J 2023년 5월 18일
One way
Data(:,:,1) = [1,2,3
4,5,6
7,8,9];
Data(:,:,2) = [11,22,33
44,55,66
77,88,99];
idxRow = [1,1
3,2];
idxCol = [1,3
1,2];
[m,n,p]=size(Data);
idx=sub2ind([m,n],idxRow,idxCol);
D=reshape(Data,[],p);
output = reshape(D(idx,:),[size(idxRow),p])
output =
output(:,:,1) = 1 3 7 5 output(:,:,2) = 11 33 77 55

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by