Remove elements within a range from an array
조회 수: 6 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!