Splitting a 1 x1 cell array every three characters?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
0 Kommentare
Antworten (2)
Image Analyst
am 13 Okt. 2015
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)
0 Kommentare
Star Strider
am 13 Okt. 2015
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!