I have a column vector with numbers 1 to 8. Under normal circumstances there are 4 consecutive values of each number, moving from 1 to 8 i.e: 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]';
The pattern starts again from one after the 8.
However, sometimes there are missing values and the vector does not look like the one above but, for example, like this:
Imperefect_sample=[1 1 2 2 2 3 3 3 3 4 5 5 5 5 6 7 7 7 7 8 8]';
My aim is to replace the first two values of each consecutive set of identical numbers with 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]'
If there are only two or less consecutive identical numbers, then those numbers should be replaced with 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]'
How can I achieve this?
according to what I understood, this will work. Keep in mind that it does not consider any occurrences greater than 4, as you did not mention this being possible. This is based on what I understood from your original 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