返回具有公差的唯一元素 [英] Return Unique Element with a Tolerance
问题描述
在Matlab中,有一个 unique
命令返回数组中的w个唯一行.这是一个非常方便的命令.
In Matlab, there is this unique
command that returns thew unique rows in an array. This is a very handy command.
但是问题是我无法为其分配公差-以双精度,我们总是必须在精度内比较两个元素.是否有内置命令返回一定范围内的唯一元素?
But the problem is that I can't assign tolerance to it-- in double precision, we always have to compare two elements within a precision. Is there a built-in command that returns unique elements, within a certain tolerance?
推荐答案
从R2015a起,终于有了一个实现此功能的我的其他答案):
As of R2015a, there is finally a function to do this, uniquetol
(before R2015a, see my other answer):
uniquetol
在公差范围内设置唯一.
< c1>与unique
相似.unique
执行精确比较,而uniquetol
使用容差进行比较.
uniquetol
Set unique within a tolerance.
uniquetol
is similar tounique
. Whereasunique
performs exact comparisons,uniquetol
performs comparisons using a tolerance.
语法很简单:
C = uniquetol(A,TOL)
使用容差TOL
返回A
中的唯一值.
C = uniquetol(A,TOL)
returns the unique values inA
using toleranceTOL
.
语义也是如此:
每个
C
值都在一个A
值的公差范围内,但是C
中没有两个元素在彼此的公差范围内.C
以升序排序.如果满足以下条件,则两个值u
和v
都在公差范围内:
abs(u-v) <= TOL*max(A(:),[],1)
Each value of
C
is within tolerance of one value ofA
, but no two elements inC
are within tolerance of each other.C
is sorted in ascending order. Two valuesu
andv
are within tolerance if:
abs(u-v) <= TOL*max(A(:),[],1)
它也可以操作"ByRows
",并且容差可以通过输入"DataScale
"来缩放,而不是通过输入数据中的最大值来缩放.
It can also operate "ByRows
", and the tolerance can be scaled by an input "DataScale
" rather than by the maximum value in the input data.
但是有一个关于解决方案唯一性的重要说明:
But there is an important note about uniqueness of the solutions:
可以有多个满足条件的有效
C
输出,"C
中没有两个元素在彼此的公差范围内".例如,交换A
中的列可以导致返回不同的解决方案,因为输入是按列的字典顺序排序的.另一个结果是uniquetol(-A,TOL)
可能不会给出与-uniquetol(A,TOL)
相同的结果.
There can be multiple valid
C
outputs that satisfy the condition, "no two elements inC
are within tolerance of each other." For example, swapping columns inA
can result in a different solution being returned, because the input is sorted lexicographically by the columns. Another result is thatuniquetol(-A,TOL)
may not give the same results as-uniquetol(A,TOL)
.
还有一个新功能ismembertol
以与上述相同的方式与ismember
相关.
There is also a new function ismembertol
is related to ismember
in the same way as above.
这篇关于返回具有公差的唯一元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!