1

我想在 MATLAB 的逻辑数组中找到所有系列的 1 和 0 的长度。这就是我所做的:

A = logical([0 0 0 1 1 1 1 0 1 1 0 0 0 0 0 0 1 1 1 1 1]);
%// Find series of ones:
csA = cumsum(A);
csOnes = csA(diff([A 0]) == -1);
seriesOnes = [csOnes(1) diff(csOnes)];
%// Find series of zeros (same way, using ~A)
csNegA = sumsum(~A);
csZeros = csNegA(diff([~A 0]) == -1);
seriesZeros = [csZeros(1) diff(csZeros)];

这有效,并给出seriesOnes = [4 2 5]and seriesZeros = [3 1 6]。然而,在我看来它是相当丑陋的。

我想知道是否有更好的方法来做到这一点。性能不是问题,因为它很便宜(A不超过几千个元素)。我正在寻找代码清晰和优雅。

如果没有更好的办法,我就把它放在一个小辅助函数中,这样我就不用看它了。

4

3 回答 3

2

您可以使用现有代码进行run-length-encoding,它为您(丑陋)工作,然后自己过滤掉您的向量。这样你的辅助函数就相当通用了,它的功能从名字就很明显了runLengthEncode

重用此答案中的代码:

function [lengths, values] = runLengthEncode(data)
startPos = find(diff([data(1)-1, data]));
lengths = diff([startPos, numel(data)+1]);
values = data(startPos);

然后,您将使用以下方法过滤掉您的向量:

A = logical([0 0 0 1 1 1 1 0 1 1 0 0 0 0 0 0 1 1 1 1 1]);
[lengths, values] = runLengthEncode(A);
seriesOnes = lengths(values==1);
seriesZeros = lengths(values==0);
于 2015-03-01T10:08:29.907 回答
1

你可以试试这个:

A = logical([0 0 0 1 1 1 1 0 1 1 0 0 0 0 0 0 1 1 1 1 1]);
B = [~A(1) A ~A(end)];                %// Add edges at start/end
edges_indexes = find(diff(B));        %// find edges
lengths = diff(edges_indexes);        %// length between edges

%// Separate zeros and ones, to a cell array
s(1+A(1)) = {lengths(1:2:end)};
s(1+~A(1)) = {lengths(2:2:end)};
于 2015-03-01T11:18:00.683 回答
0

这种strfind基于(与数字数组和字符串数组一起工作)的方法可能更容易遵循 -

%// Find start and stop indices for ones and zeros with strfind by using
%// "opposite (0 for 1 and 1 for 0) sentients" 
start_ones = strfind([0 A],[0 1]) %// 0 is the sentient here and so on
start_zeros = strfind([1 A],[1 0])
stop_ones = strfind([A 0],[1 0])
stop_zeros = strfind([A 1],[0 1])

%// Get lengths of islands of ones and zeros using those start-stop indices 
length_ones = stop_ones - start_ones + 1
length_zeros = stop_zeros - start_zeros + 1
于 2015-03-01T09:14:05.537 回答