How to count the number of elements of a cell having string and integers?

9 次查看(过去 30 天)
Hello guys,
I have a cell array of strings and integer elements as shown here:
'POLR3D,SNAPC5'
'HIST3H2BB,HIST3H2BB'
'TFAM'
0
'CYP4F2'
0
'TRNT1,NUP214,NUP160,TRNT1'
'RPS21'
'ACTR3'
0
I used this code to count the elements of this cell but doesn't work , i think because this cell have multiple types.
So is it possible to count this cell elements or need some transformations types as i tried int2str, string but non of them worked!
SS=(tt(:,2));
n3=size(SS,1);
sub_stage_col2 = cell(1, n3);
for k1 = 1:n3
sub_stage_col2{k1} = strtrim(strsplit(SS{k1,:} ,','));
end
ptws_count2 = cellfun(@(x) numel(x), sub_stage_col2);
for m1=1:n3
pathws_count2(m1,1)=ptws_count2(m1);
end
Also this cell have some repeated elements in some of its rows in rows 2 and 7: 'HIST3H2BB,HIST3H2BB', 'TRNT1,NUP214,NUP160,TRNT1 'How to remove the duplicates ?
I again appreciate any help!
  4 个评论
chocho
chocho 2018-12-16
@Image Analyst I want to count the elements of this cell array and when there is 0 put it 0 bcz i mean by 0 no element.
I attached the matfile

请先登录,再进行评论。

采纳的回答

TADA
TADA 2018-12-15
编辑:TADA 2018-12-15
because you have multiple types, most likely you will need to either selectively change to a uniforrm data type at the begining or handle the data selectively according to it's datatype (i.e using ischar/isnumeric)
I assumed the cell array you supplied is this:
{'POLR3D,SNAPC5'; 'HIST3H2BB,HIST3H2BB'; 'TFAM'; 0; 'CYP4F2'; 0; 'TRNT1,NUP214,NUP160,TRNT1'; 'RPS21'; 'ACTR3'; 0};
and I assumed it's the value of your SS variable. historically speaking, this is an ill name choice if you ask me. That being said, I got this going:
SS = {'POLR3D,SNAPC5'; 'HIST3H2BB,HIST3H2BB'; 'TFAM'; 0; 'CYP4F2'; 0; 'TRNT1,NUP214,NUP160,TRNT1'; 'RPS21'; 'ACTR3'; 0};
n3 = size(SS,1);
sub_stage_col2 = cell(1, n3);
for k1 = 1:n3
if ischar(SS{k1})
sub_stage_col2{k1} = unique(strtrim(strsplit(SS{k1} ,',')));
else
sub_stage_col2{k1} = SS{k1};
end
end
pathws_count2(:,1) = cellfun(@(x) numel(x), sub_stage_col2(:));
disp(pathws_count2);
2
1
1
1
1
1
3
1
1
1
the use of unique function gets rid of the duplicates within each row
Putting the result of cellfun in one array then iterating to copy to a second array is unnecessary, even if the number of rows in pathws_count2 and sub_stage_col2 is different (then you can set only the desired rows.
and the call to strsplit is a potential bug:
strsplit(SS{k1,:}, ',')
will be the same as calling
strsplit(SS{k1}, ',')
unless SS is a matrix with more than one column (or a row vector), in which case, you will probably get an exception since it's like calling
strsplit(SS{k1,1}, SS{k1,2}, SS{k1,3},...,',')
  3 个评论
TADA
TADA 2018-12-16
编辑:TADA 2018-12-16
You Can Save The Output Cell Array Without unique Then Invoke unique And Save That In Another Cell Array.
As For The Zero, Just Change
% Not Tested
if isnumeric(SS{k1})
if SS{k1} == 0
sub_stage_col2{k1} = '';
else
% If You Need It
sub_stage_col2{k1} = SS{k1};
end
end
I Don't Know Where You Get That Data From But Perhaps You Can Change The Way You Parse That Data To begin With To A Better Data structure

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by