Arrays 如何计算数组中一系列自然数之间的重复数
我有一个排序(上升趋势)数组 我想检查并打印每个“自然数”之间的重复数 例如:Arrays 如何计算数组中一系列自然数之间的重复数,arrays,matlab,repeat,Arrays,Matlab,Repeat,我有一个排序(上升趋势)数组 我想检查并打印每个“自然数”之间的重复数 例如: A=[1 1 1 1 1 1.2 1.6 2 2 2 2.4 2.4 2.4 2.6 3 3.5 3.6 3.8 3.9 4 4.3 4.3 4.6 5 5.02 6 7] M=tabulate(A) % get frequency table id1=mod(M(:,1),1)>0; % get indices for non integer values
A=[1 1 1 1 1 1.2 1.6 2 2 2 2.4 2.4 2.4 2.6 3 3.5 3.6 3.8 3.9 4 4.3 4.3 4.6 5 5.02 6 7]
M=tabulate(A) % get frequency table
id1=mod(M(:,1),1)>0; % get indices for non integer values
id2=M(:,2)>1; % get indices for more than one occurrence
idx=id1 & id2; % get indices that combines the two above
ans=[M(idx,1) , M(idx,2)] % show value , # of repeats
ans =
2.4000 3.0000
4.3000 2.0000
介于1和2:0之间(无重复)
在2和3之间:用2.4重复3次
在3到4:0之间
在4和5之间:2重复4.3
在5到6:0之间
在6点到7点之间
MATLAB中有任何函数可以执行此任务吗?您可以使用,甚至不需要对数组进行排序。
然后使用逻辑条件选择适当的元素。例如:
A=[1 1 1 1 1 1.2 1.6 2 2 2 2.4 2.4 2.4 2.6 3 3.5 3.6 3.8 3.9 4 4.3 4.3 4.6 5 5.02 6 7]
M=tabulate(A) % get frequency table
id1=mod(M(:,1),1)>0; % get indices for non integer values
id2=M(:,2)>1; % get indices for more than one occurrence
idx=id1 & id2; % get indices that combines the two above
ans=[M(idx,1) , M(idx,2)] % show value , # of repeats
ans =
2.4000 3.0000
4.3000 2.0000
您可以使用,甚至不需要对数组进行排序。
然后使用逻辑条件选择适当的元素。例如:
A=[1 1 1 1 1 1.2 1.6 2 2 2 2.4 2.4 2.4 2.6 3 3.5 3.6 3.8 3.9 4 4.3 4.3 4.6 5 5.02 6 7]
M=tabulate(A) % get frequency table
id1=mod(M(:,1),1)>0; % get indices for non integer values
id2=M(:,2)>1; % get indices for more than one occurrence
idx=id1 & id2; % get indices that combines the two above
ans=[M(idx,1) , M(idx,2)] % show value , # of repeats
ans =
2.4000 3.0000
4.3000 2.0000
另一种方法是使用
histc
。所以如果你的向量存储在
h = histc(a,a); % count how many times the number is there, the a should be sorted
natNumbers = (mod(a,1)==0) .* h;
nonnatNum = (mod(a,1)>0).*h;
indNN = find(natNumbers>0);
indNNN = find(nonNatNumbers>1);
resultIndex = sort([indNN indNNN]);
result = [a(resultIndex);h(resultIndex)]
然后,您可以通过检查自然数之间是否有任何数字来处理结果矩阵另一种方法是使用
histc
。所以如果你的向量存储在
h = histc(a,a); % count how many times the number is there, the a should be sorted
natNumbers = (mod(a,1)==0) .* h;
nonnatNum = (mod(a,1)>0).*h;
indNN = find(natNumbers>0);
indNNN = find(nonNatNumbers>1);
resultIndex = sort([indNN indNNN]);
result = [a(resultIndex);h(resultIndex)]
然后,您可以通过检查自然数之间是否存在任何数字来处理结果矩阵