Asked by Waqar Ali Memon
on 4 Jul 2019

Hello everyone,

I am working on creating a code that can count the same occurance of a row string.

For example,

I have a variable named as Proejct1: that contains the following data.

ADS µSOIC8

AVX 0603

AVX 0603

AVX 0603

ELN

EPC 0603

EPC 0603

EPC 0603

FAG DO214AA

FAG SOD128

FAG SOD128

ELN

FAG SOD123W

FAG DO214AC

FAG SOD123W

I want to count the occurance of the unique rows.

for example, AVS 0603 3

ELN 2

ADS µSOIC8 1 and so on.

Note: I have browsed alot but not geeting the concrete answer.

Thank you in advance.

Regards,

Waqar Ali Memon

Answer by Stephen Cobeldick
on 4 Jul 2019

Edited by Stephen Cobeldick
on 4 Jul 2019

Accepted Answer

>> P = {'ADS','µSOIC8';'AVX','0603';'AVX','0603';'AVX','0603';'ELN','';'EPC','0603';'EPC','0603';'EPC','0603';'FAG','DO214AA';'FAG','SOD128';'FAG','SOD128';'ELN','';'FAG','SOD123W';'FAG','DO214AC';'FAG','SOD123W'}

P =

'ADS' 'µSOIC8'

'AVX' '0603'

'AVX' '0603'

'AVX' '0603'

'ELN' ''

'EPC' '0603'

'EPC' '0603'

'EPC' '0603'

'FAG' 'DO214AA'

'FAG' 'SOD128'

'FAG' 'SOD128'

'ELN' ''

'FAG' 'SOD123W'

'FAG' 'DO214AC'

'FAG' 'SOD123W'

>> [~,~,id1] = unique(P(:,1));

>> [~,~,id2] = unique(P(:,2));

>> [~,X,idx] = unique([id1,id2],'rows');

>> [cnt,bin] = histc(idx,unique(idx));

>> Q = P(X,:);

>> Q(:,3) = num2cell(cnt)

Q =

'ADS' 'µSOIC8' [1]

'AVX' '0603' [3]

'ELN' '' [2]

'EPC' '0603' [3]

'FAG' 'DO214AA' [1]

'FAG' 'DO214AC' [1]

'FAG' 'SOD123W' [2]

'FAG' 'SOD128' [2]

Waqar Ali Memon
on 10 Jul 2019

See in uploaded data:

On row 9: 'FAG' 'SOD128' 2

On row 112: 'FAG' 'SOD128' 3

So instead of showing: 'FAG' 'SOD128' 2

It should show: 'FAG' 'SOD128' 5

May be u got now? :-(

Stephen Cobeldick
on 10 Jul 2019

@Waqar Ali Memon: your original data did not have third column, did not contain numeric data, and you requested to count the rows... now you want me to guess that you actually want to sum numeric values... luckily for you my magical crystal ball is working this morning:

vec = accumarray(bin,vertcat(P{:,3}));

Q(:,4) = num2cell(vec);

Giving:

>> Q

Q =

'ADS' 'µSOIC8' [1] [ 1]

'AVX' '0603' [1] [ 3]

'ELN' '' [1] [ 2]

'EPC' '' [2] [ 6]

'EPC' '0603' [1] [ 3]

'FAG' 'DO214AA' [2] [ 40]

'FAG' 'DO214AC' [1] [ 1]

'FAG' 'SOD123W' [2] [ 7]

'FAG' 'SOD128' [2] [ 5] % <--- check this

'FSL' 'LQFP-64 ePAD' [1] [ 1]

'FSL' 'LQFP80-ePad' [2] [ 2]

... etc.

Note that your uploaded data still does not give the output that you requested earlier (because the 'ADS' 'µSOIC8' third-column values sum to 1, not 4 as you requested. Very confusing).

Waqar Ali Memon
on 10 Jul 2019

Thanks Man, it worked and sorry for confusion :-).

Sign in to comment.

Answer by infinity
on 4 Jul 2019

Edited by infinity
on 4 Jul 2019

Here is a possible solution that you can refer

clear

pj1 = {

'AVX', '0601'

'AVX', '0603'

'AVX', '0603'

'EPC', '0603'

'EPC', '0603'

'FAG', 'SOD123W'}

n = length(pj1);

pj2 = cell(n,1);

for i = 1:n

pj2{i} = [pj1{i,1},' ',pj1{i,2}];

end

res = cell(n,2);

for i = 1:n

res{i,1} = pj2{i};

res{i,2} = sum(ismember(pj2,pj2{i}));

end

The result will be

res =

6×2 cell array

{'AVX 0601' } {[1]}

{'AVX 0603' } {[2]}

{'AVX 0603' } {[2]}

{'EPC 0603' } {[2]}

{'EPC 0603' } {[2]}

{'FAG SOD123W'} {[1]}

Waqar Ali Memon
on 4 Jul 2019

Thank you for your time and help. This code is also working. So, one may use both codes to get required solution. Thank you so much :-)

Best Regards,

Waqar Ali Memon

Sign in to comment.

Answer by Jos (10584)
on 4 Jul 2019

A solution with less calls to unique:

P = {'ADS','µSOIC8';'AVX','0603';'AVX','0603';'AVX','0603';'ELN','';'EPC','0603';'EPC','0603';'EPC','0603';'FAG','DO214AA';'FAG','SOD128';'FAG','SOD128';'ELN','';'FAG','SOD123W';'FAG','DO214AC';'FAG','SOD123W'}

Ptemp = strcat(P(:,1), '_', P(:,2)) ;

[~, i, j] = unique(Ptemp) ;

OUT = P(i,:) ;

OUT(:,3) = num2cell(accumarray(j, 1, [numel(i) 1], @sum, 0))

Stephen Cobeldick
on 4 Jul 2019

Although this does use fewer unique calls than my answer, it unfortunately concatenates the data together (which is exactly what I wanted to avoid). The method is not robust when the data includes an underscore (or space, or whatever character is used to separate them, if any). The method cannot distinguish between these two rows of data:

'A_B' 'C'

'A' 'B_C'

An uncritical reliance on such a method could very easily lead to an unexpected output.

Trung VO DUY's answer also suffers from exactly the same limitation.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## infinity (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/470189-counting-the-same-occurance-of-a-row-string#comment_721230

## Waqar Ali Memon (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/470189-counting-the-same-occurance-of-a-row-string#comment_721231

Sign in to comment.