What is result of x = min(A(:));

3 次查看(过去 30 天)
Richard Johnston
Richard Johnston 2015-2-6
评论: Stephen23 2015-2-6
I am looking at result of a program and value result do not look right Looking at code ( can't run test to check right now)
If (min(e(:) <= 5)
Z = 1
Else
Z=0
End
Answer seem to set a=0 There are 26 elements in the array with a value <= 5 So I am wondering is if the results z is 26 or is it equal the lowest value in the array?
  1 个评论
Stephen23
Stephen23 2015-2-6
This results in an error actually, due to unmatched parentheses in the first line: (min(e(:) <= 5). Until you show us exactly where the parentheses are, any answer is going to be meaningless.

请先登录,再进行评论。

回答(2 个)

Alex Taylor
Alex Taylor 2015-2-6
The result of
A = [4 5; 8 7];
x = min(A(:))
is:
x =
4
This is because any matrix in MATLAB can be linearly indexed from 1 to the total number of elements in the matrix. So, the operation A(:) reshapes A into an Nx1 vector, then the min operation scans from the first element to the last element of the N element vector ooking for the minimum.

Scott Webster
Scott Webster 2015-2-6
Not sure if part of your confusion is your "order of operations" i.e. the difference between min(e)<5 and min(e<5)... Here is some demo code...
>> A=1:5
A =
1 2 3 4 5
>> min(A)
ans =
1
>> A<=3
ans =
1 1 1 0 0
>> min(A<=3)
ans =
0
>>

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by