Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 所有素子矩阵的最大和_Matlab_Primes_Submatrix - Fatal编程技术网

Matlab 所有素子矩阵的最大和

Matlab 所有素子矩阵的最大和,matlab,primes,submatrix,Matlab,Primes,Submatrix,标题中给出了这个问题。我处理这个问题的方法是: 创建一个二进制矩阵B,其中1s表示输入中的素数,例如V,它是nxn非负整数矩阵 找到所有正子矩阵,包括1x1 fom B 求它们的和,返回最大的一个,子矩阵的左上角和它的大小 从这个意义上讲,我的算法的第2部分似乎有点复杂。有没有办法不用蛮力就找到它们,我想,通过for循环迭代找到它们。我希望matlab有一个函数返回我想要的 非常感谢您的帮助。这大致就是您的计划: % generate random matrix sz = [20 20]; im

标题中给出了这个问题。我处理这个问题的方法是:

  • 创建一个二进制矩阵B,其中1s表示输入中的素数,例如V,它是nxn非负整数矩阵
  • 找到所有正子矩阵,包括1x1 fom B
  • 求它们的和,返回最大的一个,子矩阵的左上角和它的大小
  • 从这个意义上讲,我的算法的第2部分似乎有点复杂。有没有办法不用蛮力就找到它们,我想,通过for循环迭代找到它们。我希望matlab有一个函数返回我想要的


    非常感谢您的帮助。

    这大致就是您的计划:

    % 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
    

    你想要所有的矩形子矩阵,还是严格的正方形子矩阵?你是在计算这些子矩阵中的素数,还是对这些素数的值求和?