Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String MATLAB中最有效的长字符串子字符串分割方法_String_Matlab_Split_Substring - Fatal编程技术网

String MATLAB中最有效的长字符串子字符串分割方法

String MATLAB中最有效的长字符串子字符串分割方法,string,matlab,split,substring,String,Matlab,Split,Substring,我正在使用MATLAB中的一个函数来比较两个基因序列并确定它们的相似性。为此,我将两个序列划分为更小的子串,方法是使用For循环遍历它们,每次移动一个核苷酸,然后将子串添加到细胞阵列中 因此,例如,子字符串长度为4的字符串ATGCAAAT不会拆分为 ATGC,AAAT 而是 ATCG、TGCA、GCAA、CAAA、AAAT 我试图使函数的执行速度更快,因为这两个for循环提供了几乎90%的执行时间,我想知道MATLAB中是否有更快的方法来实现这一点 以下是我当前使用的代码: SubstrSeq

我正在使用MATLAB中的一个函数来比较两个基因序列并确定它们的相似性。为此,我将两个序列划分为更小的子串,方法是使用For循环遍历它们,每次移动一个核苷酸,然后将子串添加到细胞阵列中

因此,例如,子字符串长度为4的字符串ATGCAAAT不会拆分为

ATGC,AAAT

而是

ATCG、TGCA、GCAA、CAAA、AAAT

我试图使函数的执行速度更快,因为这两个for循环提供了几乎90%的执行时间,我想知道MATLAB中是否有更快的方法来实现这一点

以下是我当前使用的代码:

 SubstrSequence1 = {};                                                
 SubstrSequence2 = {};
 for i = 1:length(Sequence1)-(SubstringLength-1)                
     SubstrSequence1 = [SubstrSequence1, Sequence1(i:i+SubstringLength-1)];
 end

 for i = 1:length(Sequence2)-(SubstringLength-1)                
     SubstrSequence2 = [SubstrSequence2, Sequence2(i:i+SubstringLength-1)]; 
 end
这个怎么样

str = 'ATGCAAAT';
n = 4;
strs = str(bsxfun(@plus, 1:n, (0:numel(str)-n).'));
结果是一个2D字符数组:

所以部分字符串是
strs(1,:)
strs(2,:)

如果要将结果作为字符串的单元格数组,请在末尾添加以下内容:

strs = cellstr(strs);
产生

strs = 
    'ATGC'
    'TGCA'
    'GCAA'
    'CAAA'
    'AAAT'
然后部分字符串是
strs{1}
strs{2}
等等。

这里有一种方法用于获取
子序列1
-

A = 1:numel(Sequence1);
out = cellstr(Sequence1(hankel(A(1:SubstringLength),A(SubstringLength:end)).'))
您可以按照相同的过程查找
子序列2

样本运行-

>> Sequence1 = 'ATGCAAAT';
>> SubstringLength = 4;
>> A = 1:numel(Sequence1);
>> cellstr(Sequence1(hankel(A(1:SubstringLength),A(SubstringLength:end)).'))
ans = 
    'ATGC'
    'TGCA'
    'GCAA'
    'CAAA'
    'AAAT'

一种方法是生成适当提取所需子字符串的索引矩阵:

>> sequence = 'ATGCAAAT';
>> subSequenceLength = 4;
>> numSubSequence = length(sequence) - subSequenceLength + 1;
>> idx = repmat((1:numSubSequence)', 1, subSequenceLength) + repmat(0:subSequenceLength-1, numSubSequence, 1);
>> result = sequence(idx)

    result =

        ATGC
        TGCA
        GCAA
        CAAA
        AAAT

我从汉克尔开始,但没能成功@路易斯门多我从bsxfun开始,但为时已晚!:)我相信你是从那开始的:-非常感谢,这很有效。它比我使用的for循环快大约3倍,而且比这里的其他建议稍微快一点。
>> sequence = 'ATGCAAAT';
>> subSequenceLength = 4;
>> numSubSequence = length(sequence) - subSequenceLength + 1;
>> idx = repmat((1:numSubSequence)', 1, subSequenceLength) + repmat(0:subSequenceLength-1, numSubSequence, 1);
>> result = sequence(idx)

    result =

        ATGC
        TGCA
        GCAA
        CAAA
        AAAT