MATLAB: How to add extra column that counts the occurance of each character in a cellarray

character matlab

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

Best Answer

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))),' ','');