matlab - Reduce matrix by adding every n -
not sure how explain here goes example:
a=[1 0 0 1 4 4 4 4 0 0 0 0 2 3 2 2 0 0 0 0 0 0 0 1 2 3 4 5 2 3 4 1 ] result:
b=[ 1 1 13 12 5 9 5 6]; each of elements computed adding n size submatrix inside original, in case n=2.
so b(1,1) a(1,1)+a(1,2)+a(2,1)+a(2,2), , b(1,4) a(1,7)+a(2,7)+a(1,8)+a(2,8).
visually , more clearly:
a=[|1 0| 0 1| 4 4| 4 4| |0 0| 0 0| 2 3| 2 2| ____________________ |0 0| 0 0| 0 0| 0 1| |2 3| 4 5| 2 3| 4 1| ] b sum of elements on squares, in example of size 2.
i can imagine how make loops, feels vectorizable. ideas of how done?
assume matrix has sizes multipliers of n.
if have image processing toolbox blockproc option well:
b = blockproc(a,[2 2],@(x) sum(x.data(:)))
Comments
Post a Comment