MATLAB Answers

0

How do i compare cell array values and replace with string of code letters?

Asked by Damien Williams on 7 May 2019
Latest activity Commented on by Damien Williams on 7 May 2019
% If i have a cell array like, cellArr = {'UUU'} {'CUU'} {'UUC'} {'UUG'} i
% want to create a string with appropriate code as per below eg 'FLFL'
F = {'UUU','UUC'};
L = {'UUA','UUG','CUU','CUC','CUA','CUG'};
% empty string.
amino_acid_chain = strings(1,numel(cellArr));
for i = 1:numel(cellArr)
amino_acid_chain(i) = strcmpi(cellArr(i), 'UUU');
end
The best i can come up with at the moment is to create an empty string array, then use strcmpi (to ignore case), and find a way to replace true entries with appropriate code. Two problems, 1st, i dont know how to compare {'UUU', 'UUC'} instead of just 'UUU', and 2nd, how do i convert true entries to code (F or L). Alternatively, im sure theres probably a much easier way to achieve this so any other advice would be appreciated.

  0 Comments

Sign in to comment.

1 Answer

Answer by Stephen Cobeldick on 7 May 2019
Edited by Stephen Cobeldick on 7 May 2019
 Accepted Answer

Method one:
>> C = {'UUU','CUU','UUC','UUG'};
>> F = {'UUU','UUC'};
>> L = {'UUA','UUG','CUU','CUC','CUA','CUG'};
>> Xf = ismember(C,F)
Xf =
1 0 1 0
>> Xl = ismember(C,L)
Xl =
0 1 0 1
And then use indexing:
>> Y = Xf+2*Xl
Y =
1 2 1 2
>> Z = 'FL';
>> Z(Y)
ans = FLFL
Or even something like:
>> char('F'*Xf + 'L'*Xl)
ans = FLFL
Use upper on ismember's input arguments to get case-insensitive comparisons.
Method two:
>> [~,X] = ismember(C,[F,L]);
>> V = cumsum(cellfun(@numel,{F,L}));
>> Y = 1+sum(V(:)<X)
Y =
1 2 1 2
Method three:
>> C = {'UUU','CUU','UUC','UUG'};
>> F = {'UUU','UUC'};
>> L = {'UUA','UUG','CUU','CUC','CUA','CUG'};
>> F(2,:) = {'F'};
>> L(2,:) = {'L'};
>> D = [F,L];
>> [~,X] = ismember(C,D(1,:));
>> [D{2,X}]
ans = FLFL

  3 Comments

I probably should have mentioned this, the reason i was trying to use a for loop is because i actually have 22 codes to compare the cell array against. I just put 2 in for simplicity.
Methods two and three are trivially adapted to work with an arbitrary number of cell vectors (as long as you have sensibly stored them all in one cell array):
>> C = {'UUU','CUU','UUC','UUG'};
>> D = {{'UUU','UUC'},{'UUA','UUG','CUU','CUC','CUA','CUG'},... add them here ...};
>> [~,X] = ismember(C,[D{:}]);
>> V = cumsum(cellfun(@numel,D));
>> Y = 1+sum(V(:)<X)
... etc

Sign in to comment.