Group number identification consecutive numbers

조회 수: 6 (최근 30일)
Marek Drliciak
Marek Drliciak 2022년 11월 11일
이동: Bruno Luong 2022년 11월 21일
Hello
I have millions of 1-0 data in my database. I need to detect groups of consecutive units. I would like to ask you to create the attribute "Gr_numb". An example is given in the attached file. The searched attribute is calculated there. But in Excel it is very complicated and unusable for a large database.
Thank You
  댓글 수: 6
Image Analyst
Image Analyst 2022년 11월 11일
OK I think I see now, though it is probably a poor example since the second group and third groups both have a group label of 3. I don't see why two different groups should have the same group number, but the example would have been clearer if the third group had had 4 or 5 1's in it instead of 3 (same as the prior group). You must have the Mind Reading Toolbox Bruno.
Jan
Jan 2022년 11월 11일
편집: Jan 2022년 11월 11일
@Image Analyst: The term "group number" is misleading. "Number of group members" would be clearer.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2022년 11월 11일
편집: Jan 2022년 11월 11일
index = [1 0 1 1 1 0 0 0 1 1 1 0 1 1 0 0 1 1];
[b, n] = RunLength(index);
m = n(b == 1);
Gr_numb = zeros(size(index));
GR_numb(index == 1) = RunLength(m, m);
If you do not have a C-compiler installed, use RunLength_M from this submission.
Or:
d = [true, diff(index) ~= 0]; % TRUE if values change
n = diff(find([d, true])); % Number of repetitions
m = n(index(d) == 1);
index(index == 1) = repelem(m, m)
index = 1×18
1 0 3 3 3 0 0 0 3 3 3 0 2 2 0 0 2 2
  댓글 수: 1
Marek Drliciak
Marek Drliciak 2022년 11월 21일
이동: Bruno Luong 2022년 11월 21일
Thank You very much. That was what I looking for.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2022년 11월 11일
X = [1;0;1;1;1;0;0;0;1;1;1;0;1;1;0;0;1;1;0;1;0;0;1;1;0;1;1;1;1;0;1;1;1;1;1;;0;1;1;1;1;1;1;1;1;1;0;0;0;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;0]
X = 65×1
1 0 1 1 1 0 0 0 1 1
D = diff([0;X;0]);
B = find(D>0);
E = find(D<0);
L = E-B;
G = X;
G(X==1) = repelem(L,L)
G = 65×1
1 0 3 3 3 0 0 0 3 3

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by