将MATLAB中的两个矩阵与不同的数组大小进行比较

时间:2013-10-21 15:15:17

标签: arrays matlab

所以这是我的样本数据:

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 medians进行比较,并找出Arr StarP中具有相应ID(2141865)的任何值是否在Arr medians中该ID的值的某个范围内(如果它大1.5 *或1.5 *小),如果不是那么它需要乘以或除以1.5。如果Arr StarP的值大于或小于Arr中值,那么我希望在新的平均值计算中忽略该值。

示例输出:

2141865 14.00666667

P.S。 Arr B的实际大小为198x2,Arr A为45879x2,Arr A中有许多不同的ID,而Arr B恰好有198,Arr B来源于找到Arr A中具有匹配ID的所有值的中位数并输出该中位数和ID。

1 个答案:

答案 0 :(得分: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