Remove elements within a range from an array

조회 수: 6 (최근 30일)
Daniel Morrow
Daniel Morrow 2021년 4월 2일
편집: DGM 2021년 4월 2일
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!

채택된 답변

DGM
DGM 2021년 4월 2일
편집: DGM 2021년 4월 2일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by