0

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?

Buzz
  • 516
  • 7
  • 21
  • 3
    Can you provide an example that has "two or less" consecutive numbers because the one that you posted as `Imperefect_sample` doesn't correspond at all with the result. Unless it does....? It is unclear. – Suever Jun 12 '16 at 13:48
  • @Suever Imperfect sample does have two or less consecutive identical numbers from 1, 4 6, and 8. I have added the Imperfect_sample_result. – Buzz Jun 12 '16 at 14:13
  • @Buzz why do you have 5 nans before the 2? – ibezito Jun 12 '16 at 14:15
  • Are you always guaranteed to have at least 1 occurrence of each number – Suever Jun 12 '16 at 14:32
  • @suever you should add a text file or something with an example of data you will be working, this would help – jerpint Jun 12 '16 at 14:50
  • The oeiginal data contains more than 60000 rows so I couldn't upload it. – Buzz Jun 13 '16 at 09:27

1 Answers1

1

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
jerpint
  • 399
  • 2
  • 16