【问题标题】:Find start and end point for every series consecutive number matlab查找每个系列连续数matlab的起点和终点
【发布时间】:2014-10-22 08:41:37
【问题描述】:

假设,我有A=[1 1 1 4 4 4 4 4 1 1 2 2 1 1 2 2 1 1 1 3 3 3 3 3]。系列号为B=[1:24] 我的问题是如何找到每个连续数字的开始和结束。 应该是我的答案的起点和终点是

for A=1 is 1,3;9,10;13,14;17,19
for A=2 is 11,12;15,16
for A=3 is 20,24
for A=4 is 4,8`

【问题讨论】:

    标签: matlab


    【解决方案1】:

    类似:

    n = 1;
    B = find(diff([0,A==n,0]));         %//Find where sequences of n and not  begin
    B(2:2:end) = B(2:2:end) - 1       %//Change from the beginning of not n sequence to the end of the n sequence
    reshape(B, 2, [])'
    

    或者现在您想要 2 列,这样做更容易(也更合乎逻辑):

    s = find(diff([0,A==n,0])==1);
    e = find(diff([0,A==n,0])==-1) -1;
    B = [s', e']
    

    【讨论】:

    • 是的。很Tq。我明白了
    • @danjoro 如果可能有一些问题,您需要彻底测试和调整。例如如果你最后有一个1,它还在做你想做的事吗?
    • 是的,这是一个问题。我该如何解决。我怎样才能像这样 B=[1 3; 9,10; 13,14; 17,19];。还有一个问题是A=3只能读取起点20,而终点24不能读取。
    • 得到它! tq非常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多