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