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

조회 수: 14 (최근 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