フィルターのクリア

how to delete adjacent entries from an array

1 回表示 (過去 30 日間)
Stelina
Stelina 2014 年 2 月 13 日
コメント済み: Stelina 2014 年 2 月 20 日
Hallo,
I have a long column vector A (say 30000x1) with values 1 to 100 and I want not only to delete all entries equal to the number 60 (which I can do with B= A(A~=60), but all 50 elements immediately next to 60 (previous and next, in total 100). The elements in A are randomly positioned , so some times the deleted values may 'overlap'. Do you know how I can do this?
Thank you

採用された回答

Jos (10584)
Jos (10584) 2014 年 2 月 13 日
Let N be the number of items that is to be removed before and after the locations of the value V in A. (so there are 2*N+1 items to be removed for non-overlapping regions)
A = [10 9 9 999 9 9 11 12 9 9 999 9 999 9 9 13 14]
V = 999
n = 2
% all elements 999 are to be removed, as well as the adjacent two elements before
% and after it (i.e., the 9's in the example array A above)
tf = logical(conv(double(A==V),ones(1,2*n+1),'same'))
A(tf) = []
  1 件のコメント
Stelina
Stelina 2014 年 2 月 20 日
Thank you all for the answers. It works (although only if I have single instead of double in the conv command)

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2014 年 2 月 13 日
編集済み: Andrei Bobrov 2014 年 2 月 13 日
ii = unique(bsxfun(@plus,find(A == 60),[-1 0 1]));
out = A;
out(ii(ii > 0 & ii <= numel(A))) = [];
way with Image Processing Toolbox
out = A(imerode(A ~= 60,[1;1;1]));

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by