2011-10-06 174 views
-2

在寻找如何在二维矩阵的已定义区域中找到最大值时存在问题。我也需要找到坐标。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> 

它只是找到了最大的坐标值,但我不能选择矩阵来寻找最大值的区域。

+1

-1:请显示您迄今为止尝试过的方法 - 有什么用?什么没有?在你推动我们之前,让* SOME *尝试你的作业!对于所有的“MATLAB常客”:我将不胜感激您在以下元讨论中的输入:http://meta.stackexchange.com/q/108521/168373 –

+1

考虑在进行快速搜索之前,您会问一个将会浪费人们的问题时间和杂乱的网站。 –

回答

0

你让这个比你需要的更难....没有理由使矩阵变平坦。

您正在使用maxind2sub正确的方向。有关选择区域的帮助,您可能需要查看Matlab自己的Matrix索引文档,特别是访问多个元素或逻辑索引。

0

这个问题需要用数组和索引来思考。

首先,您需要确定您感兴趣的区域。如果您没有子区域的坐标,可以使用例如, 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),并把它们转化,这样的位置和价值,他们对应于原始数组的坐标([1 1]subArray是原始数组的坐标中的[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; 

要检查一切正常,你可以用B(xOfMax,yOfMax)比较maxVal

4
% 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;