1

I have a code that sums up the 8 by 8 subcell of a 256 by 256 matrix to give a smaller matrix of the size 32 by 32. I use for loops, which make the process slow. Can this be done without using the loop? I need to use this summing code in an optimization tool, CVX, which doesn't go well with in-built MATLAB functions. So, it has to be a code without in-built finctions (sum and mean are allowed though).

img=rand(256);
m=1;
n=1;
for i=1:8:256
    for j=1:8:256
        temp=img(i:i+7,j:j+7);
        D(m,n)=sum(temp(:));
    n=n+1;
    end
    m=m+1;
    n=1;
end
A Khan
  • 85
  • 8
  • Do you have the image processing toolbox? – Luis Mendo Jan 26 '20 at 17:42
  • 1
    Same question, just asking for the average instead: https://stackoverflow.com/q/22362988/2732801 – Daniel Jan 26 '20 at 17:43
  • 1
    @Daniel Dupe, isn't it? – Luis Mendo Jan 26 '20 at 17:45
  • 1
    need to write the code which doesn't use any in-built function. i need to feed this function to an optimization package (namely, CVX) which starts freaking out when it sees in -built functions which spoil the convexity of the problem – A Khan Jan 26 '20 at 18:42
  • @AKhan: Please update your question to reflect your constrains. If I recall CVX right, some functions (including reshape) are allowed, but I guess the squeeze is the problem when you try to use this one? https://stackoverflow.com/a/22363530/2732801 – Daniel Jan 26 '20 at 19:23
  • Yes, squeeze is the problem with that approach otherwise it would have been a wonderful solution. – A Khan Jan 27 '20 at 11:12
  • @AKhan: If you are still interested in a solution, please update your question to include all relevant information. It can be reopened ( https://meta.stackexchange.com/questions/36415/how-do-you-reopen-a-closed-question ). Your problem should be solvable within the limitations of CVX. – Daniel Jan 28 '20 at 21:03
  • @Daniel: I don't have reopening rights it seems. I edited the question nevertheless. – A Khan Jan 30 '20 at 10:45

1 Answers1

1

There is already a solution, but we have to adapt it to be compatible with CVX.

squeeze(sum(sum(reshape(X,k,n/k,k,n/k),1),3))

I need to use this summing code in an optimization tool, CVX, which doesn't go well with in-built MATLAB functions.

fortunately only some of the built in functions are unusable, otherwise it would be very hard. A quote from the reference guide:

A number of Matlab’s basic linear and bilinear functions either work automatically with cvx expressions or have been extended to do so, including: conj, conv, cumsum, diag, dot, find, fliplr, flipud, flipdim, horzcat, hankel, ipermute, kron, mean, permute, repmat, reshape, rot90, sparse, sum, trace, tril, triu, toeplitz, vertcat.

reshape is allowed, this means the squeeze has to be replaced. Here squeeze is used to reshape a [1, n/k, 1, n/k] matrix to a [n/k n/k] matrix. It can be substituted using reshape:

n=6
k=2
X=magic(n)
reshape(sum(sum(reshape(X,k,n/k,k,n/k),1),3),n/k,n/k)
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • 1
    Thanks a lot. It turns out that squeeze works with CVX. But somehow it isn't mentioned in the list – A Khan Jan 31 '20 at 13:04