フィルターのクリア

Deleting an irregular "row"/"column" from 2-d array

2 ビュー (過去 30 日間)
Knut
Knut 2017 年 12 月 5 日
編集済み: Matt J 2017 年 12 月 5 日
I have a 2d array, and I want to eliminate the same number of elements in each row (or column) (e.g. 1 element), but the index of that element is itself av vector. This operation would be relevant for doing seam-carving (content-aware image resize).
Is there any neat way of doing this without my array collapsing into a 1d vector?
A = (1:3)'*(1:3);
A =
1 2 3
2 4 6
3 6 9
b = [1; 2; 2];
for r=(1:3)
tmp = A(r,:);
tmp(b(r)) = [];
Y(r,:) = tmp;
end
Y =
2 3
2 6
3 9
I seem to have a working method, but it is kind of cumbersome as I have to (?) make sure that the linear indexing into Y is strictly monotonic:
Y = A';
Y(sub2ind(size(A'), b, (1:3)')) = [];
Y = reshape(Y, size(A,2)-1, size(A,1))'
Anything neater?

回答 (1 件)

Matt J
Matt J 2017 年 12 月 5 日
編集済み: Matt J 2017 年 12 月 5 日
I think your solution is probably the best, except that I would avoid the extra calls to size() and ctranspose(). The latter are particularly expensive.
Y = A.';
[m,n]=size(Y);
Y( sub2ind([m,n], b, (1:3)) )=[];
Y = reshape(Y, m-1, n).'

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by