Reputation: 5690
I have an array which can be any size (in rows) but is always two columns wide. I would like to throw away any rows containing numbers which stray more than 1 from the median of each column.
For example:
array =
2 5
3 4
9 5
2 8
3 5
. .
. .
. .
etc
In the above example, median(array) gives [2 5]. So, for the columns above, I would expect the third and fourth rows to be eliminated, since row three contains 9 in the first column, and row four contains 8 in the second column, both of which are outside of my limit (1 away from the median). Note that I want to throw away BOTH columns if EITHER column contains a number which is not within 1 of the median for that column.
Any help would be greatly appreciated...
Upvotes: 4
Views: 2868
Reputation: 5690
I have created a solution, with some help from this link:
function newArray = removeOutliers(oldArray, driftAllowance)
% Remove elements from an array that are more than a certain amount from
% the median of an old array
r = size(oldArray, 1); % find the length of the array
r2 = 1; % a new row index for a new table
medianData = [3 5];
medianX = medianData(1);
medianY = medianData(2);
for i = 1 : r % for every row in the array
% If it is within the drift limits of the median
if oldArray(i,1) <= medianX + (driftAllowance/2)...
&& oldArray(i,1) >= medianX - (driftAllowance/2)...
&& oldArray(i,2) <= medianY + (driftAllowance/2)...
&& oldArray(i,2) >= medianY - (driftAllowance/2)
newArray(r2,:) = oldArray(i,:); % add it to a new array
r2 = r2 + 1; % move the new row index on
end
end
Upvotes: 1
Reputation: 12737
I don't have MATLAB right now, but I think this should work. You should be able to at least follow the logic.
med = median(arrray);
arrayNew = array( ( abs(array(:,1)-med(1))<=1 ) & ( abs(array(:,2)-med(1))<=2 ), : );
What the above code is doing is finding all indices where array value in both columns is at most distance 1 from median of each column. Then it selects only those rows that corresponds to these indices.
Upvotes: 4