From 1 dimensional matrix:
从1维矩阵:
list_A = [1,2,4,6,7,7,7,9,10,11,11,11,13,13,13]
How can i get the list of number that have a highest occurrence (in this case occurrence = 3)
如何获得出现次数最多的数字列表(在本例中为occurrence = 3)
The answer that i want should be like this (if we use list_A):
我想要的答案应该是这样的(如果我们使用list_A):
Ans = [7,11,13]
2 个解决方案
#1
4
use hist
:
使用hist:
I assumed your vector A
is sorted.
我假设您的矢量A已排序。
A = [1,2,4,6,7,7,7,9,10,11,11,11,13,13,13];
occ = 3;
out = A( hist(A,A) == occ )
gives:
得到:
out =
7 11 13
If A
is not sorted, you could sort it of course, or:
如果A未排序,您可以对其进行排序,或者:
A = [7,7,7,1,2,4,6,9,10,11,11,11,13,13,13]
occ = 3;
out = find( histc(A,1:max(A)) == occ )
If there are also negative numbers:
如果还有负数:
A = [7,7,7,-4,6,-9,10,-11,-11,-11,13,13,13,-20]
occ = 3;
out = find( histc(A,min(A):max(A)) == occ ) + min(A)-1
or the simple version is working too:
或简单版本也在工作:
A = sort(A);
out = A( hist(A,A) == occ )
>> out = -11 7 13
Another convenient way is to use the tabulate
function of the Statistics Toolbox, which would even work for decimal values!
另一种方便的方法是使用统计工具箱的制表功能,它甚至可用于十进制值!
A = [7.5,7.5,7.5,-4,6,-9,10,-11,-11,-11,13,13,13,-20]
occ = 3;
T = tabulate(A);
out = T(T(:,2) == occ ,1)
>> out =
-11.0000
7.5000
13.0000
#2
3
You can do it most easily with mode
. To get all values you should use the third output, which will be a cell array:
您可以使用模式轻松完成。要获取所有值,您应该使用第三个输出,它将是一个单元格数组:
[~, ~, result] = mode(list_A);
result = result{1}.';
EDIT
编辑
If your vector is already sorted and you want to exploit that (which mode
apparently doesn't do), you can do it manually with diff
, find
and max
:
如果您的矢量已经排序并且您想要利用它(哪种模式显然不起作用),您可以使用diff,find和max手动执行:
ii = find(diff([list_A inf]));
dii = diff(ii);
result = list_A(ii(find(dii==max(dii))+1));
#1
4
use hist
:
使用hist:
I assumed your vector A
is sorted.
我假设您的矢量A已排序。
A = [1,2,4,6,7,7,7,9,10,11,11,11,13,13,13];
occ = 3;
out = A( hist(A,A) == occ )
gives:
得到:
out =
7 11 13
If A
is not sorted, you could sort it of course, or:
如果A未排序,您可以对其进行排序,或者:
A = [7,7,7,1,2,4,6,9,10,11,11,11,13,13,13]
occ = 3;
out = find( histc(A,1:max(A)) == occ )
If there are also negative numbers:
如果还有负数:
A = [7,7,7,-4,6,-9,10,-11,-11,-11,13,13,13,-20]
occ = 3;
out = find( histc(A,min(A):max(A)) == occ ) + min(A)-1
or the simple version is working too:
或简单版本也在工作:
A = sort(A);
out = A( hist(A,A) == occ )
>> out = -11 7 13
Another convenient way is to use the tabulate
function of the Statistics Toolbox, which would even work for decimal values!
另一种方便的方法是使用统计工具箱的制表功能,它甚至可用于十进制值!
A = [7.5,7.5,7.5,-4,6,-9,10,-11,-11,-11,13,13,13,-20]
occ = 3;
T = tabulate(A);
out = T(T(:,2) == occ ,1)
>> out =
-11.0000
7.5000
13.0000
#2
3
You can do it most easily with mode
. To get all values you should use the third output, which will be a cell array:
您可以使用模式轻松完成。要获取所有值,您应该使用第三个输出,它将是一个单元格数组:
[~, ~, result] = mode(list_A);
result = result{1}.';
EDIT
编辑
If your vector is already sorted and you want to exploit that (which mode
apparently doesn't do), you can do it manually with diff
, find
and max
:
如果您的矢量已经排序并且您想要利用它(哪种模式显然不起作用),您可以使用diff,find和max手动执行:
ii = find(diff([list_A inf]));
dii = diff(ii);
result = list_A(ii(find(dii==max(dii))+1));