Splitting a 1 x1 cell array every three characters?
古いコメントを表示
I have a 1 x 1 cell array that is a short DNA codon sequence. I want to turn this sequence into a matrix with each column containing a single codon. How can I split this long string every three characters, and make it into a matrix?
回答 (2 件)
Image Analyst
2015 年 10 月 13 日
A 1x1 cell array is not much of an array - there's just one element! Anyway, try this:
stringInside = ca{1}; % Extract string from inside the cell array.
numChars = length(stringInside) % Get number of characters in the string. Must be a multiple of 3
% Reshape into a 3 column character array.
charArray = reshape(stringInside, numChars/3, 3)
Star Strider
2015 年 10 月 13 日
This is how I would do it:
B = {'A' 'T' 'C' 'G'}; % Bases
I = randi(4, 1, 12); % Sequence Index
S = {B(I)}; % Linear Sequence
S{:} % Display (Optional)
CM = reshape(S{:}, 3, []) % Codons In Columns
ans =
'G' 'G' 'G' 'A' 'T' 'A' 'A' 'T' 'C' 'C' 'C' 'C'
CM =
'G' 'A' 'A' 'C'
'G' 'T' 'T' 'C'
'G' 'A' 'C' 'C'
This is random, so running it will give different results each time, but the codons will match the order of those in the linear sequence.
カテゴリ
ヘルプ センター および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!