Find the nearest words and their edit distances to "MatWorks" and "MALTAB".
words = ["MatWorks""MALTAB"];
[idx,d] = knnsearch(eds,words)
idx = 2×1
3
1
d = 2×1
1
2
Get the words from the vocabulary using the returned indices.
nearestWords = eds.Vocabulary(idx)
nearestWords = 1x2 string
"MathWorks" "MATLAB"
Changing the word "MatWorks" to "MathWorks" requires one edit: an insertion. Changing the word "MALTAB" into "MATLAB" requires two edits: a deletion and an insertion.
There is only one word within the maximum edit distance from "Analysis", so the function returns NaN for the other indices. View the nearest words with valid indices.
Edit distance searcher, specified as an editDistanceSearcher object.
words — Input words string vector | character vector | cell array of character vectors
Input words, specified as a string vector, character vector, or cell array of character
vectors. If you specify words as a character vector, then the
function treats the argument as a single word.
Data Types: string | char | cell
Name-Value Pair Arguments
Specify optional
comma-separated pairs of Name,Value arguments. Name is
the argument name and Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN.
Example: knnsearch(eds,words,'K',3) finds the nearest three neighbors
in eds to the elements of words.
'K' — Number of nearest neighbors to find 1 (default) | positive integer
Number of nearest neighbors to find for each element in
words, specified as a positive integer.
'IncludeTies' — Option to include neighbors whose distance values are equal false (default) | true
Option to return neighbors whose distance values are equal, specified as
true or false.
If 'IncludeTies' is false, then the function
returns the K neighbors with the shortest edit distance, where
K is the number of neighbors to find. In this case, the function
outputs N-by-K matrices, where N is
the number of input words. To specify K, use the
'K' name-value pair argument.
If 'IncludeTies' is true, then the function also
returns the neighbors whose distances are equal to the Kth smallest
distance in the output. In this case, the function outputs cell arrays of size
N-by-1, where N is the number of input words. The
elements of the cell arrays are vectors with at least K elements. The
function sorts the neighbors in each vector in ascending order of distance.
idx — Indices of nearest neighbors in searcher matrix | cell array of vectors
Indices of nearest neighbors in the searcher, returned as a matrix or a cell array
of vectors.
If 'IncludeTies' is false, then the function
returns the K neighbors with the shortest edit distance, where
K is the number of neighbors to find. In this case, the function
outputs N-by-K matrices, where N is
the number of input words. To specify K, use the
'K' name-value pair argument.
If 'IncludeTies' is true, then the function also
returns the neighbors whose distances are equal to the Kth smallest
distance in the output. In this case, the function outputs cell arrays of size
N-by-1, where N is the number of input words. The
elements of the cell arrays are vectors with at least K elements. The
function sorts the neighbors in each vector in ascending order of distance.
Data Types: double | cell
d — Edit distances to neighbors matrix | cell array of vectors
Edit distances to neighbors, returned as a matrix or a cell array of vectors.
If 'IncludeTies' is false, then the function
returns the K neighbors with the shortest edit distance, where
K is the number of neighbors to find. In this case, the function
outputs N-by-K matrices, where N is
the number of input words. To specify K, use the
'K' name-value pair argument.
If 'IncludeTies' is true, then the function also
returns the neighbors whose distances are equal to the Kth smallest
distance in the output. In this case, the function outputs cell arrays of size
N-by-1, where N is the number of input words. The
elements of the cell arrays are vectors with at least K elements. The
function sorts the neighbors in each vector in ascending order of distance.