Maximum number of repeated values over an array
이전 댓글 표시
Hi,
i'm working with an array of thousands of elements and i've to limit the repeated values to 10.
Let say:
a=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9];
has to become:
[0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,9].
Any suggestion would be appreciated.
Thanks in advance,
Alessandro
채택된 답변
추가 답변 (1개)
a = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0, ...
5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9];
[v, n] = RunLength(a);
b = RunLength(v, min(n, 10));
If you do not have a C compiler installed, use the function RunLength_M of this submission.
Alternatively:
function out = LimitRunLength(in, nMax)
x = in(:);
d = [true; diff(x) ~= 0]; % TRUE if values change
b = x(d); % Elements without repetitions
k = find([d', true]); % Indices of changes
n = diff(k); % Number of repetitions
n = min(n, nMax); % Limit the run lengths
d = cumsum(n); % Cummulated run lengths
index = zeros(1, d(end)); % Pre-allocate
index(d(1:end-1)+1) = 1; % Get the indices where the value changes
index(1) = 1; % First element is treated as "changed" also
out = b(cumsum(index)); % Cummulated indices
% Let the output be a row vector, if the input is a row:
if size(in, 2) > 1
out = out.';
end
end
[EDITED] You ask for the indices of the removed elements:
a = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,5, ...
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9];
nMax = 10;
[v, n, idx] = RunLength_M(a);
b = RunLength_M(v, min(n, nMax));
crop = find(n > nMax);
idx = [idx, numel(a) + 1];
q = zeros(1, numel(a));
q(idx(crop) + nMax) = 1;
q(idx(crop + 1)) = -1;
removed = find(cumsum(q));
And as next alternative a straight forward loop:
del = false(size(a));
cur = NaN;
for k = 1:numel(a)
if a(k) == cur
len = len + 1;
del(k) = (len > nMax);
else
cur = a(k);
len = 1;
end
end
b = a(~del);
removed = find(del);
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!