how to add extra column that counts the occurance of each character in a cellarray
2 次查看(过去 30 天)
显示 更早的评论
Hi all,
I need to write a code that it add extra column for the number of occurance of the characters in each cell for the fourth column for the cellarray GO :
GO:
'GO:0008150' [1] [1] 'a'
'GO:0016740' [2] [2] 'b'
'GO:0006412' [2] [2] 'b'
'GO:0016787' [2] [3] 'c'
'GO:0006810' [2] [4] 'd'
'GO:0016787' [3] [3] 'c'
'GO:0004672' [3] [3] 'c'
'GO:0016779' [3] [3] 'c'
'GO:0005215' [3] [3] 'c'
'GO:0006810' [3] [4] 'd'
'GO:0004386' [3] [4] 'd'
'GO:0003774' [3] [4] 'd'
'GO:0016298' [3] [4] 'd'
'GO:0016192' [3] [5] 'e'
'GO:0006412' [3] [2] 'b'
'GO:0005215' [3] [3] 'c'
'GO:0006810' [4] [4] 'd'
'GO:0004386' [4] [4] 'd'
'GO:0003774' [4] [4] 'd'
'GO:0016298' [4] [4] 'd'
'GO:0030533' [4] [4] 'd'
'GO:0030533' [4] [4] 'd'
'GO:0016192' [4] [5] 'e'
The resulted GO cellarray should be as follows (starting from 0 value):
GO:
'GO:0008150' [1] [1] 'a' '0'
'GO:0016740' [2] [2] 'b' '0'
'GO:0006412' [2] [2] 'b' '1'
'GO:0016787' [2] [3] 'c' '0'
'GO:0006810' [2] [4] 'd' '0'
'GO:0016787' [3] [3] 'c' '1'
'GO:0004672' [3] [3] 'c' '2'
'GO:0016779' [3] [3] 'c' '3'
'GO:0005215' [3] [3] 'c' '4'
'GO:0006810' [3] [4] 'd' '1'
'GO:0004386' [3] [4] 'd' '2'
'GO:0003774' [3] [4] 'd' '3'
'GO:0016298' [3] [4] 'd' '4'
'GO:0016192' [3] [5] 'e' '0'
'GO:0006412' [3] [2] 'b' '2'
'GO:0005215' [3] [3] 'c' '5'
'GO:0006810' [4] [4] 'd' '5'
'GO:0004386' [4] [4] 'd' '6'
I tried the following code but it doesn't work anyway:
x3=[]; % for saving the resulted numbers
z=0:length(GO); % will take the numbers from this matrix
z=z';
for j=1:length(num2alph)
for k=1:length(GO)
for i=1:length(GO)
if isequal(GO{i,4},num2alph{j})
x3{i}=z(k);
else
end
end
end
end
x3=x3';
GO1=[GO x3];
where cellarray num2alph (which will compare it with the fourth column of GO array to build the numering column)is:
'a'
'b'
'c'
'd'
'e'
'f'
'g'
'h'
'i'
'j'
'k'
also I need to map the last 2 columns with each other to be in the same cell , for example: 'a' '0' ===>a0
'b' '2' ===>b2
any advice
thx
采纳的回答
Andrei Bobrov
2012-10-31
编辑:Andrei Bobrov
2012-10-31
GO = {
'GO:0008150' [1] [1] 'a'
'GO:0016740' [2] [2] 'b'
'GO:0006412' [2] [2] 'b'
'GO:0016787' [2] [3] 'c'
'GO:0006810' [2] [4] 'd'
'GO:0016787' [3] [3] 'c'
'GO:0004672' [3] [3] 'c'
'GO:0016779' [3] [3] 'c'
'GO:0005215' [3] [3] 'c'
'GO:0006810' [3] [4] 'd'
'GO:0004386' [3] [4] 'd'
'GO:0003774' [3] [4] 'd'
'GO:0016298' [3] [4] 'd'
'GO:0016192' [3] [5] 'e'
'GO:0006412' [3] [2] 'b'
'GO:0005215' [3] [3] 'c'
'GO:0006810' [4] [4] 'd'
'GO:0004386' [4] [4] 'd'
'GO:0003774' [4] [4] 'd'
'GO:0016298' [4] [4] 'd'
'GO:0030533' [4] [4] 'd'
'GO:0030533' [4] [4] 'd'
'GO:0016192' [4] [5] 'e'};
i1 = cell2mat(GO(:,3));
S = regionprops(i1, 'PixelIdxList');
i2 = zeros(numel(i1),1);
for jj = 1:numel(S)
i2(S(jj).PixelIdxList) = 0:numel(S(jj).PixelIdxList)-1;
end
GO(:,4) = regexprep(strcat(GO(:,4),cellstr(num2str(i2))),' ','');
更多回答(1 个)
Jan
2012-10-31
GO = {
'GO:0008150' [1] [1] 'a'
'GO:0016740' [2] [2] 'b'
'GO:0006412' [2] [2] 'b'
'GO:0016787' [2] [3] 'c'
'GO:0006810' [2] [4] 'd'
'GO:0016787' [3] [3] 'c'
'GO:0004672' [3] [3] 'c'
'GO:0016779' [3] [3] 'c'
'GO:0005215' [3] [3] 'c'
'GO:0006810' [3] [4] 'd'
'GO:0004386' [3] [4] 'd'
'GO:0003774' [3] [4] 'd'
'GO:0016298' [3] [4] 'd'
'GO:0016192' [3] [5] 'e'
'GO:0006412' [3] [2] 'b'
'GO:0005215' [3] [3] 'c'
'GO:0006810' [4] [4] 'd'
'GO:0004386' [4] [4] 'd'
'GO:0003774' [4] [4] 'd'
'GO:0016298' [4] [4] 'd'
'GO:0030533' [4] [4] 'd'
'GO:0030533' [4] [4] 'd'
'GO:0016192' [4] [5] 'e'};
col4 = GO(:, 4);
list = unique(col4);
for iList = 1:numel(list)
index = strcmp(col4, list{iList});
GO(index, 5) = mat2cell(transpose(0:sum(index)-1)));
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!