2011-10-06 150 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; 
相關問題