Matlab 如何计算重复元素的最大数量?
我想计算一个序列中出现的最大零数,例如,我希望上面的结果是8,它显示同时出现的最大零数,而不是总的零数16。 如何在matlab中编写代码Matlab 如何计算重复元素的最大数量?,matlab,sequence,Matlab,Sequence,我想计算一个序列中出现的最大零数,例如,我希望上面的结果是8,它显示同时出现的最大零数,而不是总的零数16。 如何在matlab中编写代码 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
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;
diff
find
diff
计算每个“转换”之间的元素数max
以获得连续数字的最大序列[1-Grid_中断(1),Grid_中断,1-Grid_中断(end)]代码>
使用diff
使用find
再次使用diff
计算每个“转换”之间的元素数
最后调用max
以获得连续数字的最大序列
请注意,如果最大序列出现在边缘,您可能会遇到问题,在这种情况下,我建议您首先对矩阵进行预处理并附加一个反转位,如下所示:[1-Grid_中断(1),Grid_中断,1-Grid_中断(end)]代码>为了提高透明度,有一个非常简单的算法
假设您可以将序列放入向量X中:
max(diff(find(diff(Grid_outage))))
编辑:丹的解决方案从大约200个元素开始更有效
为了提高透明度,有一个非常简单的算法
假设您可以将序列放入向量X中:
max(diff(find(diff(Grid_outage))))
编辑:丹的解决方案从大约200个元素开始更有效
要查找参数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
如果a(:)==x
结果=长度(a);%整个向量具有x参数
其他的
结果=最大值(查找(a~=x,1,'first')-1,长度(a)-find(a~=x,1,'last'));%边缘的最大差异
if~isempty(max(diff(find(a~=x)))-1)
如果是空的(结果)
结果=最大值(差异(查找(a~=x))-1;%体内最大差异
elseif结果
要查找参数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
如果a(:)==x
结果=长度(a);%整个向量具有x参数
其他的
结果=最大值(查找(a~=x,1,'first')-1,长度(a)-find(a~=x,1,'last'));%边缘的最大差异
if~isempty(max(diff(find(a~=x)))-1)
如果是空的(结果)
结果=最大值(差异(查找(a~=x))-1;%体内最大差异
elseif结果
+1用于显式显示简单算法,但这并没有利用惯用的Matlab及其基本内置函数。您可以很容易地在一行中完成这项工作,而且可能也会更高效。@Dan从大约200个元素开始,您的单行解决方案确实更高效:)+1表示良好的缩放解决方案+1表示显式显示简单算法,但这并没有利用惯用的Matlab及其基本内置函数。您可以很容易地在一行中完成这项工作,而且它可能也会更有效。@Dan从大约200个元素开始,您的单行解决方案确实更有效:)+1对于井缩放解决方案+1使用
用于前缀/postfixya,但可能是-1,因为我的解决方案会找到1
s的最长序列。。。如果你只想要最长的0
s序列,我现在就解决这个问题,然后在这里使用答案:我不同意链接中的答案。最好将定义q的行中的零替换为NaN。因此,您可以查找您想要的任何数字,也可以是0和1。+1使用
用于前缀/postfixya,但可能是-1,因为我的解决方案会找到1
s的最长序列。。。如果你只想要最长的0
s序列,我现在就解决这个问题,然后在这里使用答案:我不同意链接中的答案。最好将定义q的行中的零替换为NaN。因此,您可以查找您想要的任何数字,也可以是0和1。相关:嘿,谢谢,成功了!:)相关人员:嘿,谢谢,它成功了!:)+1它现在可以工作了。顺便说一句,find(a~=0)
可以替换为find(a)
@Dan您在这种情况下是正确的,但是使用find(a~=x)
您可以定义必须搜索的变量x
,如果最长的序列位于边缘,则编辑不起作用。另外:[0,0,1,0,0,0,0,0,0]返回一个空数组。@EJG89检查我的编辑。现在,它在主体、边缘中起作用,当所有值都相同+1时,它现在起作用。顺便说一句,find(a~=0)可以替换为find(a)@Dan您在这种情况下是正确的,但是使用find(a~=x)
您可以定义必须搜索的变量x
,如果最长的序列位于边缘,则编辑不起作用。另外:[0,0,1,0,0,0,0,0,0]返回一个空数组。@EJG89检查我的编辑。现在,当所有值都相同时,它在主体、边缘中工作