To find submatrix having max. sum

3 vues (au cours des 30 derniers jours)
Surya V
Surya V le 13 Juin 2015
Commenté : Image Analyst le 16 Juin 2015
The function takes matrix(A) as input and computes the sum of elements each of its submatrices, and finds the maximum sum. The submatrix is of the form contiguous set of elements of the original matrix. If there are more than one with maximum sum the function can pick any one of them. The function form is function [row, col, numrows, numcols, summa]= maxsubsum (A); where row and col specify the indexes of the top left corner of the submatrix with maximum sum , numrows and numcols are its dimensions and summa is the of the elements.
  2 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 13 Juin 2015
Can you illustrate with an example?
Muhammad Usman Saleem
Muhammad Usman Saleem le 14 Juin 2015
@Azzi , this is an example as input argument
function [row,col,numrows,numcols,summa] = maxsubsum(A)

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 13 Juin 2015
Modifié(e) : Image Analyst le 13 Juin 2015
This is trivial, at least for window with odd numbers of elements. Only slightly less trivial for even window widths. Just use conv2(). But you have to specify the size of the submatrix, in two for loops because you need to find it's size. Obviously, for an all positive matrix, the submatrix with the biggest sum would simply be the same as the entire image with the sum being the sum of all the matrix elements. If the matrix has positive and negative values, and you have to try every submatrix from a 1 by 1 to a rows-by-cols, and do that for every element in the matrix, then it gets time consuming and very complicated - basically use conv2 for every possible submatrix size which could be hundreds depending on the size of the matrix.
[rows, columns] = size(A);
overallMaxSum = -inf;
for smColumns = 1 : columns
for smRows = 1 : rows
sumMatrix = conv2(inputMatrix, ones(smRows, smColumns), 'same');
% Find max value
maxValue = max(sumMatrix);
if maxValue > overallMaxSum
numrows = smRows;
numcols = smColumns;
% Now find location of that max value.
% Window will be centered about that max, so now
% find the starting and stopping rows and columns
% for the submatrix centered at that location
% by subtracing half the window width.
end
end
end
I'm going to leave the last few lines to you since it seems to be homework.
  8 commentaires
Muhammad Usman Saleem
Muhammad Usman Saleem le 15 Juin 2015
@Image????
Image Analyst
Image Analyst le 16 Juin 2015
Muhammad, it's not that hard. Let's say your window was 5 wide. Let's say your window was centered at column 55. Can't you just draw that out and figure out where the left and right edges would be??? Let me show you:
49 50 51 52 53 54 55 56 57 58 59 <- Your columns numbers
X X X X X <- Your 5 wide window.
-2 -1 0 1 2 <- Position relative to center
So it looks like the left edge of the window is -2 to the left of the center. So find the row and column of the max value
[rowOfMax, colOfmax] = find(sumMatrix == maxValue);
But rowOfMax and colOfmax are the location of the center, so you need to subtract half the window width. But you can't take smRows/2 and smColumns/2 because you can't start at half pixels so you need to convert the 5/2 = 2.5 into 2. You do that with the floor() function. So the left edge is colOfMax - floor(cmColumns/2).
row = rowOfMax - floor(smRows/2);
col = colOfMax - floor(smColumns/2);
That pretty much explains it in excruciating detail . Stick those lines into my prior code and that pretty much completes your assignment. Please mark the Answer as Accepted. Thanks in advance.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by