Replacing small sections of a logical vector

1 回表示 (過去 30 日間)
CarrotCakeIsYum
CarrotCakeIsYum 2016 年 11 月 25 日
回答済み: Jan 2016 年 11 月 25 日
I'm working with EMG data to determine when muscles activate in a group of disabled people.
After data processing I end up with a logical vector, of 1s where the muscle in 'on', and 0s where the muscle is 'off', for example:
EMG = 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Periods of three 1s or less in a row are likely to be artefact, and I need to replace them with 0s. E.g. to make the above into:
EMG = 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Any help is greatly appreciated!

回答 (4 件)

Image Analyst
Image Analyst 2016 年 11 月 25 日
It's a one-liner if you have the Image Processing Toolbox since there is a function made specially to do that operation:
EMG = bwareaopen(EMG, 4); % Keep runs of 1's that are 4 or longer.

Alexandra Harkai
Alexandra Harkai 2016 年 11 月 25 日
There are a few different ways to do it.
This method may be frowned upon, but it nonetheless produces the required output:
n = 3; % max length of artefact periods
s = char(EMG+65); % converting 0-1 data to A-B string
for j=1:n
% get rid of periods of length j
s = strrep(s, ['A', repmat('B',1,n), 'A'], [repmat('A',1,n+2)]);
end
res = double(s)-65; % converting back to logical values

Andrei Bobrov
Andrei Bobrov 2016 年 11 月 25 日
編集済み: Andrei Bobrov 2016 年 11 月 25 日
v = bwlabel(EMG);
EMG(ismember(v,find(accumarray(v(:)+1,1)<=3)-1)) = 0;

Jan
Jan 2016 年 11 月 25 日
[B, N] = RunLength(EMG);
B(N <= 3) = 0;
EMG2 = RunLength(B, N);

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by