how many times a string is present in a cell array
3 次查看(过去 30 天)
显示 更早的评论
I have the array
a={'2';'23';'231';'2312';'23121';'231213';'3';'31'}
and the cell array
b={'2' '21' '' '' '' '' '';'3' '32' '' '' '' '' '';'2' '24' '242' '2423' '' '' '';'(34)' '(34)2' '' '' '' '' '';'4' '43' '432' '4323' '' '' '';'3' '32' '321' '3212' '32124' '321243' '';'3' '34' '343' '3432' '34323' '' '';'(34)' '(34)3' '' '' '' '' '';'2' '21' '212' '' '' '' '';'3' '32' '323' '' '' '' '';'4' '41' '413' '4132' '41321' '413213' '4132132';'3' '34' '342' '3423' '34232' '342321' '';'4' '42' '421' '4212' '42124' '' '';'4' '43' '432' '4324' '' '' '';'4' '43' '432' '4323' '43234' '' ''}
I want to know how many times the string in a are present in b
eg '2' 3 times
'3' 5 times
'23' 0 times
etc
I want as result a matrix that has in the fisrt column the strings and in the second column the times string in a are present in b
I have written this code but it is good only for a string, I don't know how to automize it
for i=1:size(b,1)
for j=1:size(b,2)
c(i,j)=strcmp(a{1,1},b{i,j})
end
end
s=sum(c)
can you help me? thanks
0 个评论
采纳的回答
Stephen23
2016-4-14
编辑:Stephen23
2016-4-14
Here it is in just three lines of code, no loops, and no third-party functions:
a = {'2';'23';'231';'2312';'23121';'231213';'3';'31'};
b = {'2','21','','','','','';'3','32','','','','','';'2','24','242','2423','','','';'(34)','(34)2','','','','','';'4','43','432','4323','','','';'3','32','321','3212','32124','321243','';'3','34','343','3432','34323','','';'(34)','(34)3','','','','','';'2','21','212','','','','';'3','32','323','','','','';'4','41','413','4132','41321','413213','4132132';'3','34','342','3423','34232','342321','';'4','42','421','4212','42124','','';'4','43','432','4324','','','';'4','43','432','4323','43234','',''};
d = cellfun(@(c)strcmp(b,c),a,'UniformOutput',false);
out = a;
out(:,2) = cellfun(@nnz,d,'UniformOutput',false)
0 个评论
更多回答(3 个)
Jos (10584)
2016-4-14
A job for COUNTMEMBER!
Download it here from the file exchange: http://www.mathworks.com/matlabcentral/fileexchange/7738-countmember
You can take a look at (short!) code and perhaps learn some new matlab tricks.
0 个评论
Jos (10584)
2016-4-14
One of the simplest ways:
a = {'2';'23';'231';'2312';'23121';'231213';'3';'31'};
b ={'2','21','','','','','';'3','32','','','','','';'2','24','242','2423','','','';'(34)','(34)2','','','','','';'4','43','432','4323','','','';'3','32','321','3212','32124','321243','';'3','34','343','3432','34323','','';'(34)','(34)3','','','','','';'2','21','212','','','','';'3','32','323','','','','';'4','41','413','4132','41321','413213','4132132';'3','34','342','3423','34232','342321','';'4','42','421','4212','42124','','';'4','43','432','4324','','','';'4','43','432','4323','43234','',''};
N = cellfun(@(x) sum(strcmp(x,b(:))),a)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!