sub2ind - get all of 3rd dimension
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have 2 matrices, one is a 2D matrix carrying an integer of labels and another a 3D matrix where each [row col] is a feature vector. I am trying to extract the feature vectors of a specific label. The result should be a matrix where either each row or column is the feature vector of a specific pixel. The sub2ind function does not allow me to easily extract it like below. I hope someone can assist me.
How else can I extract the vectors of every pixel given the row and column ?
[row, col] = find(label == 41);
% does not work
sub_feature_map = label(sub2ind(size(feature_map), row, col, :));
0 Commenti
Risposte (2)
Andrei Bobrov
il 16 Mag 2017
Modificato: Andrei Bobrov
il 16 Mag 2017
EDIT
without sub2und
l0 = label == 41;
k = size(feature_map,3);
sub_feature_map = reshape(feature_map(repmat(l0,1,1,k)),[],k);
with sub2ind (bad idea)
[m,n,k] = size(feature_map);
[ii,jj] = find(repmat(label == 41,1,1,k));
sub_feature_map = reshape(feature_map(sub2ind([m,n,k],ii,rem(jj,n),ceil(jj/n))),[],k);
1 Commento
Guillaume
il 16 Mag 2017
Modificato: Guillaume
il 17 Mag 2017
EDIT: Note that this answer is completly wrong. See comments for proper answer.
You don't need sub2ind, simply use:
[rows, cols] = find(label == 41);
sub_feature_map = feature_map(rows, cols, :);
You can then reshape that into one column vector per pixel with:
sub_feature_map = reshape(permute(sub_feature_map, [3 2 1]), [], size(sub_feature_map, 3));
2 Commenti
Guillaume
il 17 Mag 2017
How daft was I? Ignore my answer. Of course it doesn't work. You do need sub2ind. You just need to replicate the row and column vectors as many time as there are pages and replicate the page vectors accordingly:
[rows, cols] = find(label == 41);
npages = size(feature_map, 3);
sub_feature_map = feature_map(sub2ind(size(feature_map), ...
repmat(rows, npages, 1), ...
repmat(cols, npages, 1), ...
repelem((1:npages)', numel(rows))));
and then reshape.
Vedere anche
Categorie
Scopri di più su Numeric Types 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!