识别并标记连续的重复项 MATLAB

Identify and flag consecutive duplicates MATLAB

我有一个数字为 1 到 8 的列向量。在正常情况下,每个数字有 4 个连续值,从 1 到 8 即: Perfect_sample=[1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8]';

模式从 8 之后的一个开始。

但是,有时会出现缺失值并且向量看起来不像上面那个,但是,例如,像这样:

Imperefect_sample=[1 1 2 2 2 3 3 3 3 4 5 5 5 5 6 7 7 7 7 8 8]';

我的目标是用 NaN 替换每组连续相同数字的前两个值:

Perfect_sample_result=[NaN NaN 1 1 NaN NaN 2 2 NaN NaN 3 3 NaN NaN 4 4 NaN NaN 5 5 NaN NaN 6 6 NaN NaN 7 7 NaN NaN 8 8]'

如果只有两个或更少的连续相同数字,则应将这些数字替换为 NaN。

 Imperfect_sample_result=[NaN NaN NaN NaN NaN NaN 2 2 NaN NaN 3 3 NaN NaN NaN NaN NaN NaN 5 5 NaN NaN NaN NaN NaN NaN 7 7 NaN NaN NaN NaN]'

我怎样才能做到这一点?

根据我的理解,这行得通。请记住,它不会考虑任何大于 4 的事件,因为您没有提到这是可能的。这是基于我从你的原文中了解到的 post.

clc
clear
Imp=[1 1 2 2 2 3 3 3 3 4 5 5 5 5 6 7 7 7 7 8 8];
perf = Imp;
pos = 1; % position of your cursor
while(pos<max(size(perf))-2) % -2 to avoid going out of range
next = true; %check if it should go further
count = 0; % will store number of consecutive times the iith number appears

    while(next == true) %checks if the next digit in the sequence is the same
        if(perf(pos) == perf(pos+count))
            count = count+1;
        else
            next = false;
        end

    end


 if (count == 1)
     perf(pos) = NaN;
 elseif( count == 2)
     perf(pos:pos+1) = NaN;
 elseif(count == 3)
      perf(pos:pos+2)= NaN;
 elseif(count == 4)
      perf(pos:pos+1)= NaN;
  end

 pos = min(pos+ count,max(size(perf))); % passes the counter to the next value
end