How to get the count of continuous occurrence of specified name in the table

조회 수: 1 (최근 30일)
Hi,
I have the below Table:
Name5
Name2
Name2
Name1
Name3
Name2
Name2
Name2
Name2
Name5
Name3
Name3
Name2
Name2
Name2
I want to get the Name2 continuous count, and accumulated count as explained below:
For example: Name2 run continuously three times (2(row2&row3), 4(row6~9), 3(row13~15)), its max is 4, and its accumulated continuous run is 9(2+4+3). Output1:
2
4
3
output2:
Name5 -1
Name2 1
Name2 2
Name1 -1
Name3 -2
Name2 1
Name2 2
Name2 3
Name2 4
Name5 -1
Name3 -2
Name3 -3
Name2 1
Name2 2
Name2 3
Output2 explanation:
Consider Name2 as 1, and if it is continuous the number increase like 1,2,3 ...(till change from Name2 to other) and except Name2, all other weighting given as -1(it increases like -1,-2,-3,-4 etc till Name2 appear). For instance, row1 is Name5(it is -1), row 2&3 are Name2 (1,2), row4&5 Name1&Name3 (-1,-2), row 6~9 Name2(1,2,3,4) etc. My purpose is when Name2 change to other it is given -1 and till it changes to Name2 again (Name2 given as 1 and increase if it is continuous).

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 9일
a={'Name5'
'Name2'
'Name2'
'Name1'
'Name3'
'Name2'
'Name2'
'Name2'
'Name2'
'Name5'
'Name3'
'Name3'
'Name2'
'Name2'
'Name2'
'Name2'}
n=numel(a);
ii=ismember(a,'Name2')'
ii1=strfind([0 ii],[0 1])
ii2=strfind([ii 0],[1 0])
out1=(ii2-ii1+1)'
jj=1
for k=1:numel(ii1)
out2(jj:ii1(k)-1)=-(1:(ii1(k)-jj))
out2(ii1(k):ii2(k))=1:ii2(k)-ii1(k)+1
jj=ii2(k)+1;
end
out2(end+1:n)=-(1:n-ii2(k))
  댓글 수: 1
Mekala balaji
Mekala balaji 2016년 6월 14일
편집: Mekala balaji 2016년 6월 18일
Sir,
It giving error if Name2 does not exist. Now I handled for the case like:
a={'Name5'
'Name1'
'Name3'
'Name7'
'Name6'
'Name5'
'Name3'
'Name3'
}
a=[a;{'Name2'}]
and delete last row from out2 (out2(end,:)=[]). It works but is there any smart way.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Web Services에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by