分析序列matlab [英] analyzing sequences matlab

查看:70
本文介绍了分析序列matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简短的matlab函数,该函数将接收一个向量,并将向我返回最长1s序列的第一个元素的索引(我可以假定该序列由1s和0s组成).例如:

I am trying to write a short matlab function that will recieve a vector and will return me the index of the first element of the longest sequence of 1s (I can assume that the sequence consists of 1s and 0s). for example:

IndexLargeSeq([110001111100000000001111111111110000000000000000000000000000000])

IndexLargeSeq([110001111100000000001111111111110000000000000000000000000000000])

将返回21-这是最长1s序列的第一个1的索引.
谢谢
阿里尔

will return 21 - which is the index of the first 1 of the longest sequence of 1s.
thank you
ariel

推荐答案

这里是测量0的索引之间距离的另一种选择.如果根本不存在1(返回空向量),或者存在多个长度最长的序列,该代码将考虑这种情况. x是输入行向量.

Here is another option measuring distances between indices of 0s. The code takes into account situations if there are no 1s at all (returns empty vector), or if there are multiple sequences with the longest length. x is an input row vector.

idx = find([1 ~x 1]); %# indices of 0s +1
idxdiff = diff(idx); %# lengths of sequences (+1)
maxdiff = max(idxdiff);
if maxdiff == 1
    maxseqidx = []; %# no 1s at all
else
    %# find all longest sequences, may be more then one
    maxidx = find(idxdiff == maxdiff);
    maxseqidx = idx(maxidx);
end
disp(maxseqidx)

编辑:如果x可以是行向量或列向量,则可以将第一行更改为

EDIT: If x can be either row or column vector, you can change the first line to

idx = find([1; ~x(:); 1]);

在这种情况下,输出将是列向量.

The output will be a column vector in this case.

这篇关于分析序列matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆