I'm trying to multiply every element in a small matrix (let's say 2x2) with every position in a big matrix (let's say 4x4), element by element.
So I want:
1 2 3 4 1 0 3 0
1 0 1 2 3 4 0 0 0 0
0 0 'x' 1 2 3 4 = 1 0 3 0
1 2 3 4 0 0 0 0
The small matrix is applied as many times as it fits, and the multiplication is element by element. I've tried a bunch of loops, but that doesn't feel right in MATLAB, there must be prettier ways of doing it?
If you have the image processing toolbox, you can try
blkproc
:One possibility is to use
repmat
to repeat the small matrix as many times as necessary:Another possibility, which avoids
repmat
: cut up the large matrix, arrange the pieces in the third and fourth dimensions, and usebsxfun
to do the multiplication:(The two lines
T = ...
do the cutting, and are due to A. Donda.)The advantage of this approach is that, if memory is an issue, you can overwrite
B
instead of definingT
, thus saving memory: