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

Community Treasure Hunt

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

Start Hunting!

Translated by