Delete array rows if certain criteria are met

I have an array that can be of any size (in rows), but always has a width of two columns. I would like to throw away any rows containing numbers that deviate more than the median of each column.

For instance:

array = 2 5 3 4 9 5 2 8 3 5 . . . . . . etc 

In the above example, the median (array) gives [2 5]. Thus, 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 the fourth row contains 8 in the second column, both of which are outside my limit (1 away from the median ) Note that I want to drop BOTH columns if the EITHER column contains a number that is not within 1 of the median for this column.

Any help would be greatly appreciated ...

+4
source share
2 answers

I don't have MATLAB right now, but I think this should work. You should 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 does is find all the indices in which the value of the array in both columns is at a distance of no more than 1 from the median of each column. Then it selects only those rows that match these indices.

+4
source

I created a solution using 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 
+1
source

Source: https://habr.com/ru/post/1392235/


All Articles