How to count the number of elements of a cell having string and integers?
3 次查看(过去 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!
采纳的回答
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
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 Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!