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

15 次查看(过去 30 天)
% 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.

采纳的回答

Stephen23
Stephen23 2019-5-7
编辑:Stephen23 2019-5-7
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 个评论
Stephen23
Stephen23 2019-5-7
编辑:Stephen23 2019-5-7
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Types 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by