How to replace arrays containing consecutive repeated numbers by zero?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
maruljay
el 24 de Ag. de 2020
Comentada: Adam Danz
el 25 de Ag. de 2020
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.
0 comentarios
Respuesta aceptada
Adam Danz
el 24 de Ag. de 2020
Editada: Adam Danz
el 24 de Ag. de 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 comentarios
Más respuestas (1)
Image Analyst
el 24 de Ag. de 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.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!