Remove elements within a range from an array

6 views (last 30 days)
Hi,
I have an array called A, which could have the example values shown below:
A = [1 1.2 3 3.1;
1 1.3 3 3.2;
2 2.1 4 4.1;]
Now, I want to be able to remove the columns that are near-duplicates (in this case within a 0.5 range of another column) and I am having trouble doing so.
For clarity:
I want the final array to just be:
A = [1 3;
1 3;
2 4]
Any help would be greatly appreciated. ALSO: it does not matter which duplicate gets removed, as long as in the end there is only 1 column that represents the numbers within that range (so the 1 1 2 column could be removed instead of the 1.2 1.3 2.1 column). NOTE: the real array I would be operating on is variable in size so I need a solution that will work for any size array.
Thanks for the help I really appreciate it!

Accepted Answer

DGM
DGM on 2 Apr 2021
Edited: DGM on 2 Apr 2021
I'm going to assume that near-duplicate columns happen neatly and that only complete near-duplicate columns count
A = [1 1.2 3 3.1;
1 1.3 3 3.2;
2 2.1 4 4.1]
% pick some tolerance
tol=0.5;
goodcols=find([1 any(abs(diff(A,1,2))>=tol,1)]);
Agood=A(:,goodcols)
gives us:
Agood =
1 3
1 3
2 4
Of course, this doesn't account for a split-duplicate like this:
A = [1 1.2 3 3.1;
1 1.3 3 3.2;
2 3.9 4 4.1]
and the split-dpulicate column wouldn't get removed
Agood =
1 1.2 3
1 1.3 3
2 3.9 4
I don't know if that's appropriate for your task or not.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!