elimination of consecutive regions

1 回表示 (過去 30 日間)
Michal
Michal 2018 年 9 月 27 日
コメント済み: Michal 2018 年 9 月 29 日
I need to effectively eliminate consecutive regions in vector "a" or better in rows/columns of matrix "A" with length of separate ones regions greater than positive integer N <= length(A):
See following example:
N = 2 % separate consecutive regions with length > 2 are zeroed
a = [0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1]
a_elim = [0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1]
or 2D case:
N = 2
A = [1 0 1
1 1 0
1 1 0
0 0 1
1 1 1]
% elimination over columns
A_elim= 0 0 1
0 1 0
0 1 0
0 0 1
1 1 1
% elimination over rows
A_elim= 1 0 1
1 1 0
1 1 0
0 0 1
0 0 0
I am looking for effective vectorized function performing this task for size(A) ~ [100000, 1000] (over columns case).

採用された回答

Matt J
Matt J 2018 年 9 月 27 日
編集済み: Matt J 2018 年 9 月 27 日
e=ones(N+1,1);
if mod(N,2) %even mask
mask=~conv2(conv2(A,e,'valid')>=N+1 ,[zeros(N,1);e])>0;
mask=mask(N+1:end,:);
else %odd mask
mask=~(conv2( conv2(A,e,'same')>=N+1, e,'same')>0);
end
A_elim=A.*mask;
  3 件のコメント
Matt J
Matt J 2018 年 9 月 27 日
I've modified it to handle odd N.
Michal
Michal 2018 年 9 月 29 日
Hi Matt … now your solution works very well. Is faster and significantly less memory consuming than Bruno's code. Moreover, the "mask" is very useful to know.
Thanks!!!

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

その他の回答 (2 件)

Bruno Luong
Bruno Luong 2018 年 9 月 27 日
編集済み: Bruno Luong 2018 年 9 月 27 日
You could use Huffman encoding (there might be some on the FEX), but the idea is similar to this direct code:
N = 2
A = [1 0 1;
1 1 0;
1 1 0;
0 0 1;
1 1 1];
% Engine for working along the column (1st dimension)
[m,n] = size(A);
z = zeros(1,n);
Apad = [z; A; z];
d = diff(Apad,1,1);
[i1,j1] = find(d==1);
[i0,j0] = find(d==-1);
lgt = i0-i1;
keep1 = lgt <= N;
keep0 = keep1 & i0 <= m;
i1 = [i1,j1];
i0 = [i0,j0];
C1 = accumarray(i1(keep1,:),1,[m n]);
C0 = accumarray(i0(keep0,:),-1,[m n]);
Aclean = cumsum(C1+C0,1);
Aclean
If you want to filter along the 2nd dimension, transpose A, apply the above, then transpose the result Aclean.
  3 件のコメント
Bruno Luong
Bruno Luong 2018 年 9 月 27 日
編集済み: Bruno Luong 2018 年 9 月 27 日
It depends how it's implemented. If it's a C-MEX file might be, pure MATLAB Huffman, no chance.
Otherwise my code is probably quite fast (but it creates few big intermediate arrays, you might add "clear...) while the code is running when an array is finished to be used).
Why not test yourself with different methods the link you have found?
Michal
Michal 2018 年 9 月 27 日
Yes, you are right, the pure MATLAB Huffman encoding is not quite fast. I will test all options. Anyway, your code looks as very good method.
Thanks!

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


Michal
Michal 2018 年 9 月 27 日
編集済み: Michal 2018 年 9 月 27 日
good idea (only 1D) is here

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by