MATLAB —寻找二维矩阵区域中的最大值
我在寻找如何在二维矩阵的定义区域中找到最大值时遇到问题。我也需要找到坐标。
现在,我有这个:
B ... 2Dmatrix <br>
[row_val row_ind] =max(B, [], 1) ;<br>
[col_val col_ind] =max(row_val) ;<br>
[r c] =find(B==max(B(:))) ;<br>
[s_v s_i] =max(B(:)) ;<br>
[r c] =ind2sub(size(B), s_i)<br><br>
它只是找到最大值的坐标,但我无法选择矩阵的区域来查找最大值。
Im having problem in finding how to find a max value in a defined region of 2D matrix. I need to find the coordinates, too.
Right now, I have this:
B ... 2Dmatrix <br>
[row_val row_ind] =max(B, [], 1) ;<br>
[col_val col_ind] =max(row_val) ;<br>
[r c] =find(B==max(B(:))) ;<br>
[s_v s_i] =max(B(:)) ;<br>
[r c] =ind2sub(size(B), s_i)<br><br>
It just finds the coordinates of the largest value, but I can't choose the region of the matrix to look for the max value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如何使用matlab帮助? http://www.mathworks.se/help/techdoc/ref/max.html
还有一些例子。
以下是有关坐标的一些信息 http://www.mathworks.com/matlabcentral/fileexchange/8136
How with matlab help? http://www.mathworks.se/help/techdoc/ref/max.html
there is also some examples.
Here is some info about coordinates http://www.mathworks.com/matlabcentral/fileexchange/8136
你让这件事变得比你需要的更难......没有理由压平矩阵。
使用
max
和ind2sub
您走在正确的轨道上。如需选择区域的帮助,您可能需要查看 Matlab 自己的有关矩阵索引的文档,特别是有关访问多个元素或逻辑索引的文档。You're making this harder than you need to....there's no reason to flatten the matrix.
You're on the right track using
max
andind2sub
. For help with choosing the region, you might want to take a look at Matlab's own documentation on Matrix Indexing, in particular on Accessing Multiple Elements or Logical Indexing.这个问题需要从数组和索引的角度来思考。
首先,您需要确定您感兴趣的区域。如果您没有子区域的坐标,您可以使用例如 IMRECT
regionCoords
是一个带有[yMin,xMin,width,height]
的数组, 在哪里xMin
和yMin
分别是左上角的行索引和列索引。现在您可以提取一个子数组并找到最大值的位置和值
剩下的就是获取行和列坐标(使用
ind2sub
)并对其进行转换,以便它们对应于原始数组(subArray
的[1 1]
是原始数组坐标中的[xMin,yMin]
)。要检查一切是否正常,您可以将
maxVal
与B(xOfMax,yOfMax)
进行比较。This question requires thinking in terms of arrays and indices.
First, you need to identify the region you're interested in. If you don't have the coordinates of the sub-region, you can get them using e.g. IMRECT
regionCoords
is an array with[yMin,xMin,width,height]
, wherexMin
andyMin
are the row and column index of the top left corner, respectively.Now you can extract a sub-array and find the position and value of the maximum
All that's left is to get back row and column coordinates (using
ind2sub
) and to transform them so that they correspond to coordinates of the original array ([1 1]
ofsubArray
is[xMin,yMin]
in the coordinates of the original array).To check that everything worked, you can compare
maxVal
withB(xOfMax,yOfMax)
.