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!