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

조회 수: 5 (최근 30일)
chocho
chocho 2018년 12월 15일
댓글: chocho 2018년 12월 17일
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
chocho
chocho 2018년 12월 16일
Can anyone help me out plz ?

댓글을 달려면 로그인하십시오.

채택된 답변

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
chocho
chocho 2018년 12월 17일
@TADA Thanks TADA for your great help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by