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!