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

采纳的回答

Stephen23
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)

更多回答(3 个)

Orion
Orion 2016-4-14
编辑:Orion 2016-4-14
Hi Pamela,
you can to something like this
MyResult = [];
for i = 1:length(a)
x = find(strcmp(a{i},b));
MyResult{i,1} = a{i};
MyResult{i,2} = length(x);
end

Jos (10584)
Jos (10584) 2016-4-14
A job for COUNTMEMBER!
You can take a look at (short!) code and perhaps learn some new matlab tricks.

Jos (10584)
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)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by