これはあなたが計画し何を約ある:
% generate random matrix
sz = [20 20];
imax = 200;
A = randi(imax,sz);
% binary matrix of primes
B = isprime(A);
% concat both
C = cat(3,A,B);
% compute maximum number of rows&cols in each cc in B
cc = bwconncomp(B,4);
[rows,cols] = cellfun(@(ind)ind2sub(size(B),ind),cc.PixelIdxList,'UniformOutput',false);
maxwidth = max(cellfun(@(c) max(c) - min(c),cols)) + 1;
maxheight = max(cellfun(@(c) max(c) - min(c),rows)) + 1;
% find max-sum sub matrix
valMax = 0;
idxMax = [0,0];
for ii = 1:maxheight
for jj = 1:maxwidth
% generate rectangle filter
h = ones(ii,jj);
n1 = ii*jj; % number of elements in filter
% filter the concat matrix
res = imfilter(C,h);
% indexes of cc having rectangular shape
idxs = find(res(:,:,2) == n1);
if isempty(idxs)
break
end
% find max value of all relevant rectangles
[v,i] = max(res(idxs));
if v > valMax
valMax = v; % max value
[r,c] = ind2sub(size(B),idxs(i));
r = r - ceil(ii/2) + 1;
c = c - ceil(jj/2) + 1;
idxMax = [c,r,jj,ii]; % max value rect [x,y,w,h]
end
end
end
あなたはすべての長方形の部分行列、または厳密に正方形の部分行列をしたいですか?あなたはこれらの小行列内の素数の数を数えていますか、それらの素数の値を合計していますか? – beaker