矩陣中最小的元素
語法:
1 M = min(A)
%返回A的最小元素。如果A是一個向量,則min(A)返回A的最小元素。如果A是矩陣,則min(A)是包含每列最小值的行向量。如果A是一個多維數組,則min(A)沿着大小不等於1的第一個數組維度進行操作,將這些元素作為向量處理。此尺寸的尺寸變為1,而所有其他尺寸的尺寸保持不變。如果A是第一維0的空數組,則min(A)返回一個與A的大小相同的空數組。
2 M = min(A,[],dim)
%返回沿dim維度的最小元素。例如,如果A是矩陣,那么min(A,[],2)是包含每行最小值的列向量。
3 [M,I] = min(___)
%找到A的最小值的索引,並使用前面語法中的任何輸入參數將它們返回到輸出向量I中。如果最小值出現多次,則min返回與第一次出現相對應的索引。
4 C = min(A,B)
%返回從A或B中取出最小元素的數組。
5 ___ = min(___,nanflag)
%指定是否在任何前面的語法的計算中包含或省略NaN值。對於單輸入的情況,要指定nanflag而不指定dim,請使用min(A,[],nanflag)。例如,min(A,[],'includenan')包含A中的所有NaN值,而min(A,[],'omitnan')忽略它們。
Input Arguments
(1)A是輸入數組,指定為標量,向量,矩陣或多維數組。
- 如果A很復雜,那么min(A)返回幅度最小的復數。 如果幅度相等,則min(A)返回幅值最小且相位角最小的值。
- 如果A是標量,那么min(A)返回A.
- 如果A是一個0乘0的空數組,那么min(A)也是。
(2)dim是要一起操作的維度,指定為正整數標量。如果未指定值,則默認值是第一個數組維度,其大小不等於1。
dim表示長度減至1的維度。除非size(A,dim)為0,否則size(M,dim)為1,而所有其他維度的維度保持不變。如果size(A,dim)為0,那么min(A,dim)返回一個與A相同大小的空數組。
考慮一個二維輸入數組,A:
- 如果dim = 1,則min(A,[],1)返回包含每列中最小元素的行向量。
- 如果dim = 2,則min(A,[],2)返回包含每一行中最小元素的列向量。
如果dim大於ndims(A),則返回A
(3)B 是輸入數組,指定為標量,矢量,矩陣或多維數組。數字輸入A和B必須具有相同的尺寸或具有兼容的尺寸(例如,A是M乘N矩陣,B是標量或1乘N行矢量)。有關更多信息,請參閱基本操作的兼容陣列尺寸。
- 如果A和B是日期時間,持續時間或分類數組,那么它們必須是相同的大小,除非它是標量。
- A和B必須是相同的數據類型,除非是雙精度型。在這種情況下,另一個數組的數據類型可以是單個,持續時間或任何整數類型。
- 如果A和B是序數分類數組,它們必須具有相同順序的相同類別組。
(4)nanflag - NaN條件
'omitnan'(默認)| 'includenan'
NaN條件,指定為以下值之一:
- 'omitnan' - 忽略輸入中的所有NaN值。
- 'includenan' - 在計算輸入中包含NaN值。
對於datetime數組,您也可以使用'omitnat'或'includenat'來分別省略和包含NaT值。
Output Arguments
(1)M是最小值,作為標量,向量,矩陣或多維數組返回。大小(M,dim)為1,除非尺寸(A,dim)為0,否則所有其他尺寸的尺寸都與A中相應尺寸的尺寸相匹配。如果尺寸(A,dim)為0,則M是與A相同大小的空陣列。
(2)I是索引到A的最小值,作為標量,向量,矩陣或多維數組返回。我與M的大小相同。如果最小元素出現多次,那么我包含第一次出現的值的索引。
(3)C是來自A或B的最小元素,作為標量,向量,矩陣或多維數組返回。 C的大小由A和B的維度的隱式擴展確定。有關更多信息,請參閱基本操作的兼容陣列大小。
C的數據類型取決於A和B的數據類型:
- 如果A和B是相同的數據類型,則C匹配A和B的數據類型。
- 如果A或B是單一的,那么C是單一的。
- 如果A或B是一個整型數據類型,另一個是標量double,那么C將采用整型數據類型。
Examples
(1)Create a vector and compute its smallest element
A = [23 42 37 15 52]; %. M = min(A)
M = 15
(2)Create a complex vector and compute its smallest element, that is, the element with the smallest magnitude.
A = [-2+2i 4+i -1-3i]; % min(A)
ans = -2.0000 + 2.0000i
(3)Create a matrix and compute the smallest element in each column.
A = [2 8 4; 7 3 9]
A = 2 8 4 7 3 9
M = min(A)
M = 2 3 4
(4)Create a matrix and compute the smallest element in each row
A = [1.7 1.2 1.5; 1.3 1.6 1.99]
A = 1.7000 1.2000 1.5000 1.3000 1.6000 1.9900
M = min(A,[],2)
M = 1.2000 1.3000
(5)Create a matrix A and compute the smallest elements in each column as well as the row indices of A in which they appear.
A = [1 9 -2; 8 4 -5]
A = 1 9 -2 8 4 -5
[M,I] = min(A)
M = 1 4 -5 I = 1 2 2
(6)Create a matrix and return the smallest value between each of its elements compared to a scalar.
A = [1 7 3; 6 2 9]
A = 1 7 3 6 2 9
B = 5; C = min(A,B)
C = 1 5 3 5 2 5
(7)Create a matrixA
and use its column representation,A(:)
, to find the value and index of the smallest element.
A = [8 2 4; 7 3 9]
A = 8 2 4 7 3 9
A(:)
ans = 8 7 2 3 4 9
[M,I] = min(A(:))
M = 2
I = 3
%%%%%I
is the index ofA(:)
containing the smallest element.
(8)Now, use theind2sub
function to extract the row and column indices ofA
corresponding to the smallest element.
[I_row, I_col] = ind2sub(size(A),I)
I_row = 1 I_col = 2
(9) If you need only the minimum value of A
and not its index, call the min
function twice.
M = min(min(A))
M = 2
(10)Create a vector and compute its minimum, excludingNaN
values.
A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19]; M = min(A,[],'omitnan')
M = -2.9500
(11)min(A)
will also produce this result since'omitnan'
is the default option.Use the'includenan'
flag to returnNaN
.
M = min(A,[],'includenan')
M = NaN