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

6 ビュー (過去 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
Damien Williams
Damien Williams 2019 年 5 月 7 日
Thanks alot for your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by