-1

所以这是我的示例数据:

Arr StarP:
2141865 16
2141865 17
2141865 17
2141865 16
2141865 17
2141865 20
2141865 9
2141865 7
2141865 19
2141865 18
2141865 19
2141865 9
2141865 9
2141865 9
2141865 9
Arr medians:
2141865 16

目标是将 Arr StarP 与 Arr 中位数进行比较,并找出 Arr StarP 中具有相应 ID (2141865) 的任何值是否在 Arr 中位数中该 ID 的值的某个范围内(如果它大 1.5* 或小 1.5* ),如果不是,则需要乘以或除以 1.5。如果 Arr StarP 的值比 Arr 中位数大或小 2*,那么我希望在新的平均值计算中忽略该值。

示例输出:

2141865 14.00666667

PS Arr B 的实际大小是 198x2,Arr A 是 45879x2,Arr A 中有许多不同的 ID,Arr B 正好有 198,Arr B 是通过找到 Arr A 中所有值的中位数和匹配的ID 并将该中值与 ID 一起输出。

4

1 回答 1

1
function [medians, newAverage] = Medians_Koi(StarData, R, M)

    %//R is this range outside of which values must be multiplied. I'm assuming it's expressed in terms of the median, so for example if R is [0.8, 1.2] and the median is 10 then the range allowed is 8 - 12.
    %//M is what to multiply by (this really should have been explained in the question)

    %//why are you doing this with global and evalin?? This is really really bad practice and there is no reason for it. Just make the function accept input arguments and output output arguments
    %//StarData = evalin('base', 'StarP');
    %//global medians;
    %//global newAvg;

    StarData2 = StarData;

    [IDs, ~, Groups_1] = unique(StarData(:,1),'stable');
    medians = [IDs, accumarray(Groups_1, StarData(:,2), [], @median)];

    for g = 1:size(Group_1,1)
        toMultiply = (StarData(:,1)==IDs(g)) & (StarData(:,2) < medians(g)*R(1));
        toDivide = (StarData(:,1)==IDs(g)) & (StarData(:,2) > medians(g)*R(2));
        StarData2(toMultiply,2) = StarData(toMultiply,2) .* M(1);
        StarData2(toDivide,2) = StarData(toDivide,2) ./ M(2);
    end

    newAverage = [IDs, accumarray(Groups_1, StarData2(:,2), [], @mean)];

end
于 2013-10-22T06:34:15.550 回答