How to replace arrays containing consecutive repeated numbers by zero?
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a nested cell array that consists of vectors as shown in Fig. below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/350858/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/350861/image.png)
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.
댓글 수: 0
채택된 답변
Adam Danz
2020년 8월 24일
편집: Adam Danz
2020년 8월 24일
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
추가 답변 (1개)
Image Analyst
2020년 8월 24일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!