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
Matlab:在缺失值处切割向量并创建新向量_Matlab_Vector - Fatal编程技术网

Matlab:在缺失值处切割向量并创建新向量

Matlab:在缺失值处切割向量并创建新向量,matlab,vector,Matlab,Vector,我想写一个Matlab脚本。 在我的例子中,我有一个向量a=[1 3 4 5 7 8 9 10 11 13 14 15 16 17 19 20 21] 现在我想在缺少数字的点(这里缺少数字2、6、12、18)自动切割向量 因此,我想要向量[1]和[3 4 5]和[7 8 9 10 11]和[13 14 15 16 17]和[19 20 21]。如你所见,新向量有不同的长度 我考虑过使用for循环,但我不确定如何编写这些新向量 感谢您的帮助:)这是一种方法: s = [find(diff(A(:)

我想写一个Matlab脚本。 在我的例子中,我有一个向量a=[1 3 4 5 7 8 9 10 11 13 14 15 16 17 19 20 21]

现在我想在缺少数字的点(这里缺少数字2、6、12、18)自动切割向量

因此,我想要向量[1]和[3 4 5]和[7 8 9 10 11]和[13 14 15 16 17]和[19 20 21]。如你所见,新向量有不同的长度

我考虑过使用for循环,但我不确定如何编写这些新向量

感谢您的帮助:)

这是一种方法:

s = [find(diff(A(:).')>1) numel(A)]; %'// detect where consecutive difference exceeds 1
s = [s(1) diff(s)]; %// sizes of groups
result = mat2cell(A(:).', 1, s); %'// split into cells according to those sizes
在您的示例中,这给出了

>> celldisp(result)
result{1} =
     1
result{2} =
     3     4     5
result{3} =
     7     8     9    10    11
result{4} =
    13    14    15    16    17
result{5} =
    19    20    21

另一种方法(以不同方式计算组大小):

这是一种方法:

s = [find(diff(A(:).')>1) numel(A)]; %'// detect where consecutive difference exceeds 1
s = [s(1) diff(s)]; %// sizes of groups
result = mat2cell(A(:).', 1, s); %'// split into cells according to those sizes
在您的示例中,这给出了

>> celldisp(result)
result{1} =
     1
result{2} =
     3     4     5
result{3} =
     7     8     9    10    11
result{4} =
    13    14    15    16    17
result{5} =
    19    20    21

另一种方法(以不同方式计算组大小):


一个带有
diff
cumsum
Accumaray
-

out = accumarray(cumsum([0 ; diff(A(:))~=1])+1,A(:),[],@(x) {x})
样本运行-

>> A
A =
     1     3     4     5     7     8     9    10    ... 
            11    13    14    15    16    17    19    20    21
>> celldisp(out)
out{1} =
     1
out{2} =
     3
     4
     5
out{3} =
     7
     8
     9
    10
    11
out{4} =
    13
    14
    15
    16
    17
out{5} =
    19
    20
    21

一个带有
diff
cumsum
Accumaray
-

out = accumarray(cumsum([0 ; diff(A(:))~=1])+1,A(:),[],@(x) {x})
样本运行-

>> A
A =
     1     3     4     5     7     8     9    10    ... 
            11    13    14    15    16    17    19    20    21
>> celldisp(out)
out{1} =
     1
out{2} =
     3
     4
     5
out{3} =
     7
     8
     9
    10
    11
out{4} =
    13
    14
    15
    16
    17
out{5} =
    19
    20
    21

非常感谢你。太棒了:)非常感谢你。太棒了:)