필터 지우기
필터 지우기

Splitting a 1 x1 cell array every three characters?

조회 수: 1 (최근 30일)
JE
JE 2015년 10월 13일
답변: Star Strider 2015년 10월 13일
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
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
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.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by