フィルターのクリア

How to replace arrays containing consecutive repeated numbers by zero?

2 ビュー (過去 30 日間)
maruljay
maruljay 2020 年 8 月 24 日
コメント済み: Adam Danz 2020 年 8 月 25 日
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.

採用された回答

Adam Danz
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 件のコメント
maruljay
maruljay 2020 年 8 月 25 日
編集済み: maruljay 2020 年 8 月 25 日
Thank you so much. It worked like a charm. You just saved me tonnes of hours of work.
Your code explanation is crystal clear.
Adam Danz
Adam Danz 2020 年 8 月 25 日
Glad it worked, thanks for letting us know!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
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.
  1 件のコメント
maruljay
maruljay 2020 年 8 月 25 日
Thanks for the insight. I have accepted the answer given by Adam Danz. It worked.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by