How to realize multiple Filter with matlab programming

topic description

A rookie of matlab programming asks all the great gods to give you an idea to create an entry in the Filter data filter with matlab programming

sources of topics and their own ideas

there is a raw data on hand, no tags, only numbers, the second column of which is the heartbeat
now you need to drop the entry with a heartbeat of zero on Filter

.

I used to masturbate in other languages. I have no ideas at all.
the idea given by the boss is to extract increment

related codes

/ / Please paste the code text below (do not replace the code with pictures)
part of the data: (no tags, only numbers are heartbeats)
0.00-1-1-127-1-1 52-1
0.25 0-1-1-127-1-1 52-1
0.500 -1-1-12 7-1-1 51-1
0.75 0-1-1-12 7-1-1 51-1
1.00-1-1-12 7-1-1 50-1
1.25 0-1-1-12 7-1-1 50-1
1.500 -1-1-12 7-1-1 50-1
1.750-1-1-12 7-1-1 52-1
2.00-1-1-12 7-1 52-1
2.25 0-1-1-12 7-1 54-1
2.500 -1-1-127-1 53-1
2.75 133-1-1-127-1-1 54-1
3.00133-1-1-127-1 56-1
3.25 130-1-1-127-1 54-1
3.50 130 -1-1-12 7-1-1 56-1
3.75 126-1-1-12 7-1-1 57-1
4.00 126-1-1-12 7-1 56-1
4.25 121-1-1-12 7-1-1 56-1
4.50 121 -1-1-12 7-1-1 56-1
4.75 120-1-1-12 7-1-1 52-1

what result do you expect? What is the error message actually seen?

Filter condition:
1. When a data heartbeat of 0 is detected, if calculated from this entry, the cumulative number of entries of 0 is less than or equal to 20, and these entries will be deleted in batch;
2. If the subsequent cumulative 0 entries are greater than 20 and less than 80, calculate the average: (non-zero entry before the start of 0 entry + the first non-zero entry after the end of cumulative 0 entry) / 2
then put the calculated average into these entries

it"s a little complicated to find a train of thought and related commands. Thank you for kneeling

Jan.08,2022

if your database is M , that is

M = [
0.00 0 -1 -1 -127 -1 -1 52 -1
0.25 0 -1 -1 -127 -1 -1 52 -1
0.50 0 -1 -1 -127 -1 -1 51 -1
0.75 0 -1 -1 -127 -1 -1 51 -1
....
4.50 121 -1 -1 -127 -1 -1 56 -1
4.75 120 -1 -1 -127 -1 -1 52 -1
];
The most direct idea of

is to use a simple and rude for loop:


MSub = M(:,2); % 
skipCountCache = 0; % 0
finalSkipCount = 0; % 
accumsum = 0; %
for i = 1:numel(MSub)
    if M(i) == 0
        skipCountCache = skipCountCache + 1;
        if skipCountCache > 80 % 80
            break;
        end
        continue;
    end

    if skipCountCache > 20
        skipCountCache = 0; % 20
    else                
        finalSkipCount = finalSkipCount + skipCountCache; % finalSkipCount
        skipCountCache = 0; % 
    end
            
    accumsum = accumsum + MSub(i);
end

finalSkipCount = finalSkipCount + skipCountCache; 0finalSkipCount
average = accumsum / (i - finalSkipCount);
The

for loop is easy to understand and write, but not efficient, especially if you have a large amount of data.

at this point, we can create a filter consisting of 0 and 1 (essentially a href=" https://uk.mathworks.com/help/matlab/ref/logical.html" rel= "nofollow noreferrer" > logical array ). Then use find and diff to process the filter.

MSub = M(:,2);
zeroFilter = MSub == 0; % 
boundaryFinder = diff(zeroFilter); % ;0110
openingZeroIndices = find(boundaryFinder == 1); % 00index-1
closingZeroIndices = find(boundaryFinder == -1); % 00index
endWithZero = false; 
if MSub(1) == 0 % 0
    openingZeroIndices = [0; openingZeroIndices];
end
if MSub(end) == 0 % 0
    endWithZero = true;
    closingZeroIndices = [closingZeroIndices; numel(MSub)];
end

assert(numel(openingZeroIndices) == numel(closingZeroIndices)); % openingZeroclosingZero

zeroGroupSizes = closingZeroIndices - openingZeroIndices;  % 0
assert(all(zeroGroupSizes>0)); % 0

terminateIndex = openingZeroIndices(zeroGroupSizes >= 80); % 080

if isempty(terminateIndex) % 080
    if endWithZero
        terminateIndex = openingZeroIndices(end);
    else
        terminateIndex = numel(MSub);
    end
end

finalSkipCount = sum(zeroGroupSizes(zeroGroupSizes <= 20)); % 
average = sum(MSub(1:terminateIndex)) / (terminateIndex - finalSkipCount); % 



this is really beyond expectations. Thank you!

Menu