how many times a string is present in a cell array

1 visualización (últimos 30 días)
pamela sulis
pamela sulis el 14 de Abr. de 2016
Respondida: Jos (10584) el 14 de Abr. de 2016
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

Respuesta aceptada

Stephen23
Stephen23 el 14 de Abr. de 2016
Editada: Stephen23 el 14 de Abr. de 2016
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)

Más respuestas (3)

Orion
Orion el 14 de Abr. de 2016
Editada: Orion el 14 de Abr. de 2016
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) el 14 de Abr. de 2016
A job for COUNTMEMBER!
You can take a look at (short!) code and perhaps learn some new matlab tricks.

Jos (10584)
Jos (10584) el 14 de Abr. de 2016
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)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by