how to count number of repeating's in data series.
44 次查看(过去 30 天)
显示 更早的评论
for example my matrix A=[22 24 24 36 36 36 48 48 48 48 24 33 22 22]; i need to count how many twos, threes, four time repeating's etc
0 个评论
回答(4 个)
Massimo Zanetti
2016-10-3
编辑:Massimo Zanetti
2016-10-3
This code
A = [22 24 24 36 36 36 48 48 48 48 24 33 22 22]';
C = accumarray(A,1);
I = unique(A);
counts = [I,C(I)]
returns
out = 22 3
24 3
33 1
36 3
48 4
Andrei Bobrov
2016-10-4
编辑:Andrei Bobrov
2016-10-4
A=[22 24 24 36 36 36 48 48 48 48 24 33 22 22];
V = A(:);
[~,~,c] = unique(V);
t = diff([0;c])~= 0;
ix = cumsum(t);
out = [V(t),accumarray(ix(:),1)];
out2 = [num2cell((1:max(out(:,2)))'),...
accumarray(out(:,2),out(:,1),[],@(x){sort(x(:)')})];
3 个评论
Sylwia Kaduk
2020-5-21
Dear Andrei, 4 years later your code is very helpful in part of my PhD. Thanks a lot.
Adrian Stannard
2016-10-3
编辑:Adrian Stannard
2016-10-4
I think I understand what you want - it is the successive re-occurrences of a number.
Array=[22 24 24 36 36 36 48 48 48 48 24 33 22 22];
Array(find(diff(Array)==0))
This returns only the repeated elements from the Array after the first occurrence.
You could go further, for example use:
counts=hist(Array(find(diff(Array)==0)))
This gives you the how many times they are repeated. Alternatively:
Array2 = Array(find(diff(Array)==0));
Array3= zeros (size(Array2));
for i = 1:length(Array2)
Array3(i) = sum(Array2==Array2(i));
end
Array3=Array3+1
Array2=Array2([1,diff(Array2)]~=0);
Array3=Array3([1,diff(Array3)]~=0);
will return:
24 36 48 22
2 3 4 2
5 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!