這個問題需要用數組和索引來思考。
首先,您需要確定您感興趣的區域。如果您沒有子區域的座標,可以使用例如, 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]
,其中xMin
和yMin
分別左上角的行和列索引,陣列。
現在你可以提取子陣列和發現的最大
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
。
-1:請顯示您迄今爲止嘗試過的方法 - 有什麼用?什麼沒有?在你推動我們之前,讓* SOME *嘗試你的作業!對於所有的「MATLAB常客」:我將不勝感激您在以下元討論中的輸入:http://meta.stackexchange.com/q/108521/168373 –
考慮在進行快速搜索之前,您會問一個將會浪費人們的問題時間和雜亂的網站。 –