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

3 ビュー (過去 30 日間)
Mekala balaji
Mekala balaji 2016 年 6 月 9 日
編集済み: Mekala balaji 2016 年 6 月 18 日
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 Center および File ExchangeDatabase Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by