Delete rows from a cell given a specific condition - Matlab

I have a cell type big-variable sorted out by FIRM (A(:,2)) and I want to erase all the rows in which the same firm doesn’t appear at least 5 times in a row. In this example, A:
FIRM
1997 'ABDR' 0,56 464 1641 19970224
1997 'ABDR' 0,65 229 9208 19970424
1997 'ABDR' 0,55 125 31867 19970218
1997 'ABDR' 0,53 31 33103 19970206
1997 'ABDR' 0,70 464 46008 19971023
1997 'ABD' 0,06 435 8077 19970311
1997 'ABD' 0,00 1503 44994 19970804
1997 'ABFI' 2,07 1549 46532 19971209
I would like to get, A:
1997 'ABDR' 0,56 464 1641 19970224
1997 'ABDR' 0,65 229 9208 19970424
1997 'ABDR' 0,55 125 31867 19970218
1997 'ABDR' 0,53 31 33103 19970206
1997 'ABDR' 0,70 464 46008 19971023
Can someone help me please? Thanks a lot.

 Accepted Answer

Working just with the alpha column,
>> c={'ABDR';'ABDR';'ABDR';'ABDR';'ABDR';'ABD';'ABD';'ABFI'};
>> [u,~,i2]=unique(c);
>> [n,bin]=histc(i2,1:length(u));
>> c(bin==find(n>=5))
ans =
'ABDR'
'ABDR'
'ABDR'
'ABDR'
'ABDR'
>>

5 Comments

@dpb But I want to eliminate the whole row!
Yes, it is not working!
Yes, the logic array is the rows to save...I figured you'd recognize what to do with that--
A=A(bin==find(n>=5),:);
Maybe it's a little easier to see if save the index vector independently--
ix=(bin==find(n>=5)); % create an index vector of rows to save
A=A(ix,:); % keep those rows, all columns
NB: that saving desired rows is the same net result as deleting rows not wanted.
Yes, it is not working!
Well, need to see what isn't working and what the problem(s) are...can't see your terminal from here, unfortunately.
I am sorry. I didn't mean to say 'it's not working!' the way it sounded. I am working on the code, I will let you know if I have any problem, or doubt and in case I do, I will clearly present my doubts. Thank you :)

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel Computing in Help Center and File Exchange

Asked:

on 25 Jun 2014

Commented:

on 25 Jun 2014

Community Treasure Hunt

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

Start Hunting!