分析序列matlab

发布于 2025-01-04 03:34:34 字数 284 浏览 0 评论 0原文

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

<块引用>

IndexLargeSeq([110001111100000000001111111111110000000000000000000000000000000])

将返回 21 - 这是最长 1 序列中第一个 1 的索引。
谢谢你
爱丽儿

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])

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习惯成性 2025-01-11 03:34:34

就是这样:

% input:
A = [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1]';

% replace 0 with 2 because the next command doesn't work with '0' as values
A(A == 0) = 2;

% accumulate data sets
B = [A(diff([A; 0]) ~= 0), diff(find(diff([0; A; 0])))];

% maximize second column where first column == 1
maxSeq = max(B(B(:, 1) == 1, 2));

% get row of B where first column == 1 && second column == maxSeq
row = find(B(:,1) == 1 & B(:,2) == maxSeq, 1);

% calculate the index of the first 1s of this longest sequence:
idx = sum(B(1:(row-1),2)) + 1

idx 是您要查找的值(索引),maxSeq 是这个 1 序列的长度。 A 必须是行向量。

如果您想了解数据集是如何累积的(命令 B = ...),请查看此处:如何累积数据集?

There you go:

% input:
A = [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1]';

% replace 0 with 2 because the next command doesn't work with '0' as values
A(A == 0) = 2;

% accumulate data sets
B = [A(diff([A; 0]) ~= 0), diff(find(diff([0; A; 0])))];

% maximize second column where first column == 1
maxSeq = max(B(B(:, 1) == 1, 2));

% get row of B where first column == 1 && second column == maxSeq
row = find(B(:,1) == 1 & B(:,2) == maxSeq, 1);

% calculate the index of the first 1s of this longest sequence:
idx = sum(B(1:(row-1),2)) + 1

idx than is the value (the index) you are looking for, maxSeq is the length of this sewuence of 1s. A has to be a row-vector.

If you want to understand how the datasets are accumulated (the command B = ...), look here: How to accumulate data-sets?.

和我恋爱吧 2025-01-11 03:34:34

这是测量 0 索引之间距离的另一个选项。该代码考虑了根本没有 1(返回空向量)或存在多个具有最长长度的序列的情况。 x 是输入行向量。

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可以是行向量或列向量,您可以将第一行更改为

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

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

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)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文