How to replace arrays containing consecutive repeated numbers by zero?

2 views (last 30 days)
I have a nested cell array that consists of vectors as shown in Fig. below:
I want to go through each cell array and check it if contains atleast 50% of consecutive repetated numbers. If the cell array contains such numbers, then I want to fill the entire cell array with zeros.
See example below. In this case all the elements of cell array contains consecutive repeated numbers. I want to replace the entire cell array with zeros.
Note: The reason why I mentioned 50% is that, there were many cell arrays where the first 36,000 or so elements were normal but the other half (36,000) contained consecutive repeated numbers.

Accepted Answer

Adam Danz
Adam Danz on 24 Aug 2020
Edited: Adam Danz on 24 Aug 2020
For some reason I'm drawn to detecting / counting consecutive numbers. I must have answered more than 5 questions similar to this one and every time I come up with a different convoluted solution that is usually way too difficult to read without dissecting it. This one's no exception. Let's see if it works with your data.
% Demo data
out = {[5;5;5;1;2;3];[1;2;2;2;5;5;5];[1;5;5;5;1];[0;1;1;0;1;1]};
% out{1} = out{2} = out3{3} = out{4} =
% 5 1 1 0
% 5 2 5 1
% 5 2 5 1
% 1 2 5 0
% 2 5 1 1
% 3 5
% 5
% Compute the length of the largest consecutive segment divided by
% the length of the full vector.
p = cellfun(@(v)max(accumarray(cumsum(diff(v([2,1:end]))~=0)+1,1))/numel(v), out);
% p =
% 0.5
% 0.42857
% 0.6
% 0.33333
% Replace entire vectors in "out" that contain a segment of consecutive values
% equal to or longer than 50% of the length of the vector, with zeros.
out(p>=0.5) = cellfun(@(v){zeros(size(v))},out(p>=0.5))
% out{1} = out{2} = out3{3} = out{4} =
% 0 1 0 0
% 0 2 0 1
% 0 2 0 1
% 0 2 0 0
% 0 5 0 1
% 0 5
% 5
The much-easier-to-read, unpacked, loop version of the cellfun() would look like
p = nan(size(out));
for i = 1:numel(out)
diffOut = diff(out{i}([2,1:end])); % 0s indicate a consecutive value.
nonConsecCount = cumsum(diffOut~=0); % Group consec. vals. with 0s and non-consec-vals with cumulative integers
consecCount = accumarray(nonConsecCount+1, 1); % The number of consec values for each segment
p(i) = max(consecCount)/numel(out{i}); % largest number of consec. / length of vector
end
  2 Comments
maruljay
maruljay on 25 Aug 2020
Edited: maruljay on 25 Aug 2020
Thank you so much. It worked like a charm. You just saved me tonnes of hours of work.
Your code explanation is crystal clear.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 24 Aug 2020
Use a for loop and diff(). If you can't figure it out, attach a small chunk of your cell array in a .mat file.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!