Extract 3rd Dimension values of a Matrix using sub2ind

4 visualizzazioni (ultimi 30 giorni)
A is a Matrix of size 15x12x480
ii = size 13x1 (specific row numbers) e.g. [6,7,8,10,12.....]
jj = size 13x1 (specific column numbers) e.g. [2,4,6,9,11.....]
I want to extract the values of A matrix for ii,jj locations upto 3 rd dimension
%%% THE CODE ISN'T WORKING
Extract_A = A(sub2ind(size(A), ii(:,1),jj(:,1),:));
Anyone has a solution?
Thanks in advance

Risposte (1)

Walter Roberson
Walter Roberson il 7 Ott 2020
sub2ind() does not accept ':' as an input: it only accepts numbers.
The subscript vectors you pass must all be the same size as well.
If you want the combination of all rows listed in ii with all columns listed in jj with all 480 pages, then you would just use
A(ii, jj, :)
If what you are trying to do is like
cat(4, A(ii(1), jj(1), :), A(ii(2), jj(2), :), A(ii(3), jj(3), :), ...)
producing a 1 x 1 x 480 x 13 array, then you will need to repmat() the ii and jj .
Another approach:
szA = size(A);
page_bases = sub2ind(szA, ii, jj, ones(size(ii)));
pagesize = prod(szA(1:end-1));
pageoffsets = (0:szA(end)-1).'.*pagesize;
B = A(page_bases + pageoffsets);
B would be size(A,3) x length(ii)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by