Stumped on matlab function
Ältere Kommentare anzeigen
Write a function called maxsubsum that takes a matrix A as an input, computes the sum of elements in each of its submatrices, and finds the submatrix that has the maximum sum. (For the purposes of this exercise, we define a submatrix as a matrix formed by a contiguous set of elements of the original matrix.) If there are more than one with the same maximum sum, the function can pick any one of them. Note that the entire matrix is considered to be a submatrix of itself, but because some elements of the matrix may be negative, it may not have the maximum sum. The function is defined like this:
function [row,col,numrows,numcols,summa] = maxsubsum(A)
where row and col specify the indexes of the top left corner of the submatrix with the maximum sum, numrows and numcols are its dimensions, and summa is the sum of its elements.
My attempt:
function [ row,col,numrow,numcol,summa ] = maxsubsum( a )
n=0;
e=max(sum(a(:,:,:)));
f=max(e);
while (e<f)
n=n+1; %to get the position of the max sum among the other individual max sums
end
g=n;
b=a(:,:,n);
c=size(b);
numrow=c(1);
numcol=c(2);
summa=sum(b);
end
1 Kommentar
Walter Roberson
am 8 Dez. 2015
A different person is discussing the same requirement; see http://uk.mathworks.com/matlabcentral/answers/259208-find-submatrix-with-the-maximum-sum
Antworten (0)
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!