MATLAB: Counting the Same Occurance of a row string

counthelpMATLABrow string

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

Best 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]