mink - Find k smallest elements of array - MATLAB (original) (raw)
Find k
smallest elements of array
Syntax
Description
[B](#d126e1114494) = mink([A](#d126e1114237),[k](#d126e1114339))
returns the k
smallest elements of A
.
- If
A
is a vector, thenmink
returns a vector containing thek
smallest elements ofA
. - If
A
is a matrix, thenmink
returns a matrix whose columns contain thek
smallest elements of each column ofA
. - If
A
is a multidimensional array, thenmink
returns thek
smallest elements along the first dimension whose size does not equal 1.
[B](#d126e1114494) = mink([A](#d126e1114237),[k](#d126e1114339),[dim](#d126e1114361))
determines the k
smallest elements of A
along dimension dim
.
[B](#d126e1114494) = mink(___,'ComparisonMethod',[c](#d126e1114422))
optionally specifies how to compare elements of A
for any of the previous syntaxes. For example,mink(A,k,'ComparisonMethod','abs')
returns thek
smallest elements of A
according to their absolute values.
[[B](#d126e1114494),[I](#d126e1114524)] = mink(___)
finds the indices of the smallest k
values ofA
and returns them in I
.
Examples
Compute the smallest 3 elements of a vector.
Compute the smallest 3 elements of each row of a matrix.
A = 5×5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
B = 5×3
1 8 15
5 7 14
4 6 13
3 10 12
2 9 11
Compute the 2 smallest elements of a complex vector according to their magnitude, and return the indices where they are located in the input vector.
A = [2-2i 5+i -7-3i -1+i]
A = 1×4 complex
2.0000 - 2.0000i 5.0000 + 1.0000i -7.0000 - 3.0000i -1.0000 + 1.0000i
[B,I] = mink(A,2,'ComparisonMethod','abs')
B = 1×2 complex
-1.0000 + 1.0000i 2.0000 - 2.0000i
Input Arguments
Input array, specified as a vector, matrix, or multidimensional array.
- If
A
is a vector, thenmink
returns a vector containing thek
smallest elements ofA
. - If
A
is a matrix, thenmink
returns a matrix whose columns contain thek
smallest elements of each column ofA
. - If
A
is a multidimensional array, thenmink
returns thek
smallest elements along the first dimension whose size does not equal 1.
If A
has type categorical
, then it must be ordinal.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| categorical
| datetime
| duration
Complex Number Support: Yes
Number of minima to return, specified as a positive integer scalar. Ifk
is greater than or equal to the number of elements in the operating dimension, then mink
sorts the input array along that dimension.
Operating dimension, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.
Consider an m
-by-n
input matrix,A
:
mink(A,k,1)
computes thek
smallest values in each column ofA
and returns ak
-by-n
matrix.mink(A,k,2)
computes thek
smallest values in each row ofA
and returns anm
-by-k
matrix.
Comparison method, specified as one of the following:
'auto'
— Compare elements of inputA
byreal(A)
whenA
is real, and byabs(A)
whenA
is complex.'real'
— Compare elements of inputA
byreal(A)
whenA
is real or complex. IfA
has elements with equal real parts, then useimag(A)
to break ties.'abs'
— Compare elements of inputA
byabs(A)
whenA
is real or complex. IfA
has elements with equal magnitude, then useangle(A)
in the interval (-π,π] to break ties.
Output Arguments
Output array, returned as a scalar, vector, matrix, or multidimensional array. mink
returns the k
elements in order from smallest to largest.
Index array, returned as a vector, matrix, or multidimensional array.I
is the same size as B
. If the output array B
contains repeated elements, then the order of their indices in I
matches the order in which they appear in the input array.
Extended Capabilities
Themink
function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
- For input argument
A
:- If you do not specify a dimension, the code generator operates along the first dimension of the input array that is variable size or whose size does not equal 1. If this dimension is variable size at code generation time and is 1 at run time, a run-time error can occur. To avoid this error, specify the dimension.
- In the generated code, the input array remains complex, even if all of its elements have zero-valued imaginary parts. Under these circumstances, the results produced by the generated code might differ from those produced by MATLAB. See Code Generation for Complex Data with Zero-Valued Imaginary Parts (MATLAB Coder).
Version History
Introduced in R2017b
The mink
function shows improved performance for numeric data, including complex data, and logical data when_k
_ is at least 512 and is at least 8% of the operating dimension length.
For example, this code returns the smallest 5000 elements of each column in a numeric matrix containing 10,000 rows. The code is about 3.75x faster than in the previous release.
function timingTest A = rand(1e4,10); for i = 1:120 M = mink(A,5000); end end
The approximate execution times are:
R2024b: 1.05 s
R2025a: 0.28 s
The code was timed on a Windows® 11, AMD EPYC™ 74F3 24-Core Processor @ 3.19 GHz test system using thetimeit
function.