Counting the Same Occurance of a row string

1 回表示 (過去 30 日間)
Waqar Ali Memon
Waqar Ali Memon 2019 年 7 月 4 日
コメント済み: Waqar Ali Memon 2019 年 7 月 10 日
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 件のコメント
infinity
infinity 2019 年 7 月 4 日
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 2019 年 7 月 4 日
Thank you for reply. Yes, that looks like the same as you have mentioned.
Regards,
Waqar Ali

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2019 年 7 月 4 日
編集済み: Stephen23 2019 年 7 月 4 日
>> 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 件のコメント
Stephen23
Stephen23 2019 年 7 月 10 日
編集済み: Stephen23 2019 年 7 月 10 日
@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 2019 年 7 月 10 日
Thanks Man, it worked and sorry for confusion :-).

サインインしてコメントする。

その他の回答 (2 件)

infinity
infinity 2019 年 7 月 4 日
編集済み: infinity 2019 年 7 月 4 日
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 件のコメント
Waqar Ali Memon
Waqar Ali Memon 2019 年 7 月 4 日
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

サインインしてコメントする。


Jos (10584)
Jos (10584) 2019 年 7 月 4 日
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 件のコメント
Stephen23
Stephen23 2019 年 7 月 4 日
編集済み: Stephen23 2019 年 7 月 4 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by