sub2ind - get all of 3rd dimension
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Andrei Bobrov
2017-5-16
编辑:Andrei Bobrov
2017-5-16
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);
Guillaume
2017-5-16
编辑:Guillaume
2017-5-17
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 个评论
Guillaume
2017-5-17
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!