How to get the min and max values of a randomized matrix?

12 次查看(过去 30 天)
Hellow. I'm currently making a 20x20 matrix where each value of the matrix is a random number between 0-128, as well as fincing the largest and smallest values generated by said matrix. However, whenever I use the M=min/max(A) functions, my min/max values keep remaining the same (97 for min and 116 for max) despite the matrix generating numbers greater and smaller than those two values. Could someone explain why this is happening?

采纳的回答

Chunru
Chunru 2022-9-19
A = randi([0 128], [20, 20])
A = 20×20
90 85 13 116 87 14 4 110 116 18 37 100 124 114 24 120 109 127 65 1 62 117 52 33 100 116 40 86 74 29 93 46 0 28 60 75 114 13 46 84 78 54 73 97 4 41 0 79 6 62 117 34 28 27 28 81 55 27 31 46 29 44 66 36 99 53 28 70 55 105 80 2 110 112 26 19 9 120 11 87 52 20 17 128 60 45 72 78 98 15 41 102 81 0 78 21 115 53 71 102 127 82 118 56 19 123 50 15 41 8 43 16 127 53 78 4 38 94 33 83 26 92 105 114 46 73 65 28 67 44 38 73 56 85 98 56 20 97 128 88 106 100 23 25 114 95 54 6 56 67 38 68 21 63 8 118 12 126 127 62 0 75 51 22 123 9 108 70 14 42 18 114 87 48 45 33 120 74 77 14 77 80 101 11 91 103 95 23 108 11 61 22 119 32 32 100 72 70 95 123
maxA = max(A(:))
maxA = 128
minA = min(A(:))
minA = 0
  2 个评论
Anthony Koning
Anthony Koning 2022-9-20
Thank you so much, changing min/max(A) to min/maxA = min/max(A(:)) worked like a charm. But if you don't mind, could you explain why one works while the other keeps listing the same values?
Chunru
Chunru 2022-9-20
I am not sure what you have exactly done since you did not post your code.
Here is my guess: max(A(:)) or max(A, [], 'all') find max of all elements of A while max(A) find the max along column of A.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by