在matlab中使用hist过滤点

时间:2013-12-14 14:42:30

标签: matlab

我有一个矢量。我想删除异常值。我得到了bin,没有bin中的值。我想根据每个bin中元素的数量删除所有点。

数据:

d1 =[
      360.471912914169
      505.084636471948
       514.39429429184
      505.285068055647
      536.321181755858
      503.025854206322
      534.304229816684
      393.387035881967
      396.497969729985
      520.592172434431
      421.284713703215
      420.401106087984
       537.05330275495
      396.715779872694
       514.39429429184
      404.442344469518
      476.846474245118
      599.020867750031
      429.163139144079
      514.941744277933
      445.426761656729
      531.013596812737
      374.977332648255
      364.660115724218
      538.306752697753
      519.042387479096
      1412.54699036882
      405.571202133485
      516.606049132218
      2289.49623498271
      378.228766753667
      504.730621222846
      358.715764917016
      462.339366699398
      512.429858614816
      394.778786157514
                   366
      498.760463549388
      366.552861126468
       355.37022947906
      358.308526273099
      376.745272034036
      366.934599077274
        536.0901883079
       483.01740134285
      508.975480745389
      365.629593988233
      536.368800360349
      557.024236456548
      366.776498701866
      501.007025898839
      330.686029339009
      508.395475983019
      429.563732174866
      2224.68806802212
      534.655786464525
      518.711297351426
      534.304229816684
      514.941744277933
       420.32368479542
      367.129404978681
      525.626188464768
      388.329756778952
      1251.30895065927
      525.626188464768
      412.313764019587
      513.697381733643
      506.675438520558
      1517.71183364959
      550.276294237722
      543.359917550053
      500.639590923451
      395.129864728041];

直方图计算:

[nelements,centers] = hist(d1);

nelements=55    13     0     0     1     1     1     0     0     2

我想删除所有小于5的点(在nelements中)。这意味着只有前两个元素(55,13)仍然存在。 matlab中是否有任何功能。

4 个答案:

答案 0 :(得分:3)

您可以按照以下方式进行:

threshold = 5;
bin_halfwidth = (centers(2)-centers(1))/2;
keep = ~any(abs(bsxfun(@minus, d1, centers(nelements<threshold))) < bin_halfwidth , 2);
d1_keep = d1(keep);

答案 1 :(得分:1)

这样做你想要的吗?

binwidth = centers(2)-centers(1);
centersOfRemainingBins = centers(nelements>5);
remainingvals = false(length(d1),1);
for ii = 1:length(centersOfRemainingBins )
    remainingvals = remainingvals | (d1>centersOfRemainingBins (ii)-binwidth/2 & d1<centersOfRemainingBins (ii)+binwidth/2);
end
d_out = d1(remainingvals); 

答案 2 :(得分:1)

我不知道这个问题的Matlab函数,但我认为,那个跟随代码的函数是你在找什么:

sizeData = size(data);
function filter_hist = filter_hist(data, binCountRemove)
if or(max(sizeData) == 0, binCountRemove < 1)
    disp('Error input!');
    filter_hist = [];
    return;
end
[n, c] = hist(data);
sizeN = size(n);
intervalSize = c(2) - c(1);
if sizeData(1) > sizeData(2)
    temp = transpose(data);
else
    temp = data;
end
for i = 1:1:max(sizeN)
    if n(i) < binCountRemove
        a = c(i) - intervalSize / 2;
        b = c(i) + intervalSize / 2;
        sizeTemp = size(temp);
        removeInds = [];
        k = 0;
        for j = 1:1:max(sizeTemp)
            if and(temp(j) > a, less_equal(temp(j), b) == 1)
                k = k + 1;
                removeInds(k) = j;
            end
        end
        temp(removeInds) = [];
    end
end
filter_hist = transpose(temp);

%Determines when 'a' less or equal to 'b' by accuracy
function less_equal = less_equal(a, b)
delta = 10^-6; %Accuracy
if a < b
    less_equal = 1;
    return;
end
if abs(b - a) < delta
    less_equal = 1;
    return;
end
less_equal = 0;

答案 3 :(得分:1)

你可以做这样的事情

nelements=nelements((nelements >5))