how to count number of repeating's in data series.

21 ビュー (過去 30 日間)
shashika iresh
shashika iresh 2016 年 10 月 3 日
コメント済み: Sylwia Kaduk 2020 年 5 月 21 日
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

回答 (4 件)

Massimo Zanetti
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
  1 件のコメント
shashika iresh
shashika iresh 2016 年 10 月 3 日
no dear, i need to find the repeating(double, triple, four time, etc of same number. for a example above matrix 2 doubles(24 24,22 22),and only 1 triple(36 36 36) ,and only one four times(48 48 48 48). li

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


Andrei Bobrov
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 件のコメント
Andrei Bobrov
Andrei Bobrov 2016 年 10 月 13 日
Thank you Adrian!
Sylwia Kaduk
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
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 件のコメント
Guillaume
Guillaume 2016 年 10 月 4 日
編集済み: Guillaume 2016 年 10 月 4 日
@Adrian,
Please use the {}Code format button for the code (or just put two spaces before each line) instead of putting blank lines between each line of code.
edit: Thanks!
shashika iresh
shashika iresh 2016 年 10 月 5 日
please check for this it give wrong answer. A =[24 24 24 24 36 36 36 48 48 48 48 48 48 48 28 24 24 22 22 22 22 22 22 22 22 22 22]; Array2 =
24 36 48 24 22
Array3 =
5 3 7 5 10

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


Thorsten
Thorsten 2016 年 10 月 4 日
編集済み: Thorsten 2016 年 10 月 4 日
N(i) is the the number of occurrences of series of i numbers in A:
N = zeros(numel(A), 1);
i = 1;
while(i) < numel(A)
Ni = 1; val = A(i);
while i < numel(A) && A(i+1) == A(i)
i = i + 1; Ni = Ni + 1;
end,
N(Ni) = N(Ni) + 1;
i = i + 1;

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by