MATLAB —寻找二维矩阵区域中的最大值

发布于 2024-12-08 16:16:36 字数 361 浏览 1 评论 0原文

我在寻找如何在二维矩阵的定义区域中找到最大值时遇到问题。我也需要找到坐标。

现在,我有这个:

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

囚你心 2024-12-15 16:16:36
% extract region of interest
BRegion = B(rowStart:rowEnd, colStart:colEnd);

% find max value and get its index
[value, k] = max(BRegion(:));
[i, j] = ind2sub(size(BRegion), k);

% move indexes to correct spot in matrix
i = i + rowStart-1;
j = j + colStart-1;
% extract region of interest
BRegion = B(rowStart:rowEnd, colStart:colEnd);

% find max value and get its index
[value, k] = max(BRegion(:));
[i, j] = ind2sub(size(BRegion), k);

% move indexes to correct spot in matrix
i = i + rowStart-1;
j = j + colStart-1;
尸血腥色 2024-12-15 16:16:36

如何使用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

千紇 2024-12-15 16:16:36

你让这件事变得比你需要的更难......没有理由压平矩阵。

使用 maxind2sub 您走在正确的轨道上。如需选择区域的帮助,您可能需要查看 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 and ind2sub. 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.

川水往事 2024-12-15 16:16:36

这个问题需要从数组和索引的角度来思考。

首先,您需要确定您感兴趣的区域。如果您没有子区域的坐标,您可以使用例如 IMRECT

%# create a figure and display your 2D array (B)
figure,imshow(B,[])
regionCoords = wait(imrect);

%# round the values to avoid fractional pixels
regionCoords = round(regionCoords);

regionCoords 是一个带有 [yMin,xMin,width,height] 的数组, 在哪里xMinyMin 分别是左上角的行索引和列索引。

现在您可以提取一个子数组并找到最大值的位置和值

xMin = regionCoords(2);
yMin = regionCoords(1);
xMax = regionCoords(2) + regionCoords(4) - 1;
yMax = regionCoords(1) + regionCoords(3) - 1;
subArray = B(xMin:xMax,yMin:yMax);

%# transform subArray to vector so that we get maximum of everything
[maxVal,maxIdx] = max(subArray(:));

剩下的就是获取行和列坐标(使用ind2sub)并对其进行转换,以便它们对应于原始数组(subArray[1 1] 是原始数组坐标中的[xMin,yMin])。

%# for the size of the subArray: use elements 4 and 3 of regionCoords
%# take element 1 of maxIdx in case there are multiple maxima
[xOfMaxSubArray,yOfMaxSubArray] = ind2sub(regionCoords([4 3]),maxIdx(1));

xOfMax = xOfMaxSubArray + xMin - 1;
yOfMax = yOfMaxSubArray + yMin - 1;

要检查一切是否正常,您可以将 maxValB(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

%# create a figure and display your 2D array (B)
figure,imshow(B,[])
regionCoords = wait(imrect);

%# round the values to avoid fractional pixels
regionCoords = round(regionCoords);

regionCoords is an array with [yMin,xMin,width,height], where xMin and yMin 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

xMin = regionCoords(2);
yMin = regionCoords(1);
xMax = regionCoords(2) + regionCoords(4) - 1;
yMax = regionCoords(1) + regionCoords(3) - 1;
subArray = B(xMin:xMax,yMin:yMax);

%# transform subArray to vector so that we get maximum of everything
[maxVal,maxIdx] = max(subArray(:));

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] of subArray is [xMin,yMin] in the coordinates of the original array).

%# for the size of the subArray: use elements 4 and 3 of regionCoords
%# take element 1 of maxIdx in case there are multiple maxima
[xOfMaxSubArray,yOfMaxSubArray] = ind2sub(regionCoords([4 3]),maxIdx(1));

xOfMax = xOfMaxSubArray + xMin - 1;
yOfMax = yOfMaxSubArray + yMin - 1;

To check that everything worked, you can compare maxVal with B(xOfMax,yOfMax).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文