如果你有一个MATLAB数组,如下所示:
A = [1,1,1,1,2,2,2,2,3,3,3,4,4,5]
我希望能够过滤此数组,以便删除频率较低的元素。
换句话说,是否有一种简单的方法可以删除阵列中具有特定低频的元素,例如< = 2?
在这种情况下:
结果将是:
[1,1,1,1,2,2,2,2,3,3,3]
干杯
答案 0 :(得分:3)
我可能会这样做(希望语法很好:))
function array= ClearElementsWithLowOccurence(array,minimalFrequency)
elements = unique(array);
indecesToRemove = [];
for i = 0:length(elements)
indeces = find(array==elements(i));
if (length(indeces) < minimalFrequency)
indecesToRemove = [indecesToRemove indeces];
end;
end;
array(indecesToRemove) = [];
答案 1 :(得分:3)
这是一个快速的方法。 A
不需要排序,数字可以是任何数字。
A = [1,1,1,1,2,2,2,2,3,3,3,4,4,5];
%# count the numbers in A (use unique so that the array
%# remains at a decent size even if the values are very different)
[uniqueEntries,~,idx] = unique(A);
counts = histc(idx,1:max(idx));
%# remove all the numbers whose count is less or equal than two
A(ismember(A,uniqueEntries(counts<=2))) = []
答案 2 :(得分:-1)
考虑到您发布的问题不属于单个域,但几乎可以在任何地方遇到,我会使用伪代码。我希望这不是一个问题/烦恼。
这是你可以做的事情
拿变量
现在,对于遇到的每个(新)ELEMENT,请执行以下操作:
最后,您最终得到了所需的数组。