how many times a string is present in a cell array

1 回表示 (過去 30 日間)
pamela sulis
pamela sulis 2016 年 4 月 14 日
回答済み: Jos (10584) 2016 年 4 月 14 日
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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by