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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by