Filter löschen
Filter löschen

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 Ansicht (letzte 30 Tage)
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?

Antworten (1)

Matt J
Matt J am 18 Mai 2023
Bearbeitet: Matt J am 18 Mai 2023
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

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by