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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2015년 12월 8일

댓글:

2015년 12월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by