Counting the Same Occurance of a row string

1 Ansicht (letzte 30 Tage)
Waqar Ali Memon
Waqar Ali Memon am 4 Jul. 2019
Kommentiert: Waqar Ali Memon am 10 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
  2 Kommentare
infinity
infinity am 4 Jul. 2019
Hello,
What is the type of "Proejct1"? is it look like this
Proejct1 = {
'AVX', '0601'
'AVX', '0603'
'AVX', '0603'
'EPC', '0603'
'EPC', '0603'}
Waqar Ali Memon
Waqar Ali Memon am 4 Jul. 2019
Thank you for reply. Yes, that looks like the same as you have mentioned.
Regards,
Waqar Ali

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 4 Jul. 2019
Bearbeitet: Stephen23 am 4 Jul. 2019
>> 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]
  10 Kommentare
Stephen23
Stephen23 am 10 Jul. 2019
Bearbeitet: Stephen23 am 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
Waqar Ali Memon am 10 Jul. 2019
Thanks Man, it worked and sorry for confusion :-).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

infinity
infinity am 4 Jul. 2019
Bearbeitet: infinity am 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]}
  1 Kommentar
Waqar Ali Memon
Waqar Ali Memon am 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

Melden Sie sich an, um zu kommentieren.


Jos (10584)
Jos (10584) am 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))
  1 Kommentar
Stephen23
Stephen23 am 4 Jul. 2019
Bearbeitet: Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by