【问题标题】:How to count maximum number of recurring elements? [duplicate]如何计算重复元素的最大数量? [复制]
【发布时间】:2014-07-21 11:25:49
【问题描述】:
Grid_outage(:,1) = 1;
Grid_outage(:,2) = 1;
Grid_outage(:,3) = 1;
Grid_outage(:,4) = 0;
Grid_outage(:,5) = 0;
Grid_outage(:,6) = 0;
Grid_outage(:,7) = 0;
Grid_outage(:,8) = 0;
Grid_outage(:,9) = 0;
Grid_outage(:,10) = 0;
Grid_outage(:,11) = 0;
Grid_outage(:,12) = 1;
Grid_outage(:,13) = 0;
Grid_outage(:,14) = 1;
Grid_outage(:,15) = 0;
Grid_outage(:,16) = 0;
Grid_outage(:,17) = 1;
Grid_outage(:,18) = 0;
Grid_outage(:,19) = 1;
Grid_outage(:,20) = 0;
Grid_outage(:,21) = 0;
Grid_outage(:,22) = 0;
Grid_outage(:,23) = 1;
Grid_outage(:,24) = 0;

我想计算一个序列中出现的最大零数,例如我希望上面的结果是 8,它显示了一起出现的最大零个数,而不是 16 个零的总数。 我如何在 matlab 中编写代码

【问题讨论】:

标签: matlab sequence


【解决方案1】:

为了透明的目的,一个非常简单的算法。

假设您可以将序列放入向量 X:

% Create X containing some zeros.
X = round(rand(30,1));

% Use a counter to count the number of sequential zeros.
count = 0;
% Use a variable to keep the maximum.
max_count = 0;

% Loop over every element
for ii=1:length(X);
    % If a zero is encountered increase the counter
    if(X(ii)==0)
        count=count+1;
    % If no zero is encountered check if the number of zeros in the last sequence was largest.
    elseif count>max_count
        max_count=count;
        count=0;
    % Else just reset the counter
    else
        count=0;
    end
end
% Check if the last number of the vector exceeded the largest sequence.
if(count>max_count)
   max_count=count;
end

编辑:Dan 的解决方案从大约 200 个元素开始更有效。

【讨论】:

  • +1 用于明确显示一个简单的算法,但这并没有利用使用惯用的 Matlab 和它的基本内置函数。您可以很容易地在一行中完成此操作,而且效率可能也会更高。
  • @Dan 从约 200 个元素开始,您的单线解决方案确实更高效 :) +1 用于良好扩展的解决方案
【解决方案2】:
max(diff(find(diff(Grid_outage))))
  1. 使用diff查找连续数字序列的变化位置
  2. 使用find获取发生这种情况的实际索引号
  3. 再次使用diff 计算每个“过渡”之间的元素数量
  4. 最后调用max得到最大的连续数字序列。

请注意,如果最大的序列出现在边缘,您可能会遇到麻烦,在这种情况下,我建议您首先将反转位添加到矩阵中,如下所示:[1-Grid_outage(1), Grid_outage, 1-Grid_outage(end)];

【讨论】:

  • +1 使用; 作为前缀/后缀
  • 是的,但也许是 -1,因为我的解决方案也会找到最长的 1s 序列...我现在就解决这个问题
  • 如果您只想要0s 的最长序列,那么请在这里使用答案:stackoverflow.com/questions/7850341/…
  • 我不同意链接中的答案。将定义 q 的行中的零替换为 NaN 会更好。因此,您可以查找任何您想要的数字,包括 0 和 1。
【解决方案3】:

为了找出参数x在一个序列中一起出现的次数,可以使用:

if a(:) == x
    result = length(a); % Whole vector has the x parameter
else
    result = max(find(a~=x,1,'first') - 1, length(a) - find(a~=x,1,'last')); % Maximum difference in the edge   
    if ~isempty(max(diff(find(a~=x))) - 1)
        if isempty(result)
            result = max(diff(find(a~=x))) - 1; % Maximum difference in the body
        elseif result < max(diff(find(a~=x))) - 1
            result = max(diff(find(a~=x))) - 1; % Maximum difference in the body
        end;
    end;
end;

【讨论】:

  • +1 现在可以使用了。顺便说一句 find(a~=0) 可以替换为 find(a)
  • @Dan 适合这种情况,但使用 find(a~=x) 您可以定义必须搜索的变量 x
  • 在最长序列位于边缘的地方不起作用。另外:[0,0,1,0,0,0,0,0,0,0] 返回一个空数组。
  • @EJG89 检查我的编辑。现在它在身体、边缘和所有值都相同时工作
猜你喜欢
  • 2014-11-05
  • 1970-01-01
  • 2016-03-07
  • 2021-12-22
  • 2021-12-12
  • 2020-12-01
  • 1970-01-01
  • 2016-05-12
  • 2012-05-06
相关资源
最近更新 更多