0
votes

I would like to do an element-wise matrix multiplication using the following 2x2x3 matrix in MATLAB

>> filter_1

filter_1(:,:,1) =

     0     1
     0     0


filter_1(:,:,2) =

     1     0
     0     1


filter_1(:,:,3) =

     0     0
     1     0

This matrix will turn out to zero some elements in some matrix dimensions if used in an element-wise multiplication. For example, given another 2x2x3 matrix:

>> frames_original{1}

ans(:,:,1) =

   92   87
   93   93


ans(:,:,2) =

   69   66
   72   71


ans(:,:,3) =

   42   40
   40   43

If I do the element-wise matrix multiplication, it will let some values remain in the resulting matrix, the others will turn out to zero:

>> filtered=double(frames_original{1}).*filter_1

filtered(:,:,1) =

     0    87
     0     0


filtered(:,:,2) =

    69     0
     0    71


filtered(:,:,3) =

     0     0
    40     0

However, this only works if both matrices are of the same size (2x2x3). Now, suppose that I have a big matrix, like a 1500x1500x3 matrix. How to 'slide' my 2x2x3 window performing the element-wise multiplication dealing with the matrix borders accordingly? If using N-D convolution, it doesn't work, as MATLAB deals with this operation as an even-dimensional-window convolution, and what I want is an element-wise multiplication.

Is there any way to do element-wise matrix multiplication using one big matrix and another smaller in MATLAB?

EDIT: The solution for Element wise multiplication of matrices of differing dimensions doesn't work for me because it involves a different element-wise multiplication and requires reshaping, which is what I don't want.

1
filter_1 = repmat(filter_1, [750 750 1]); Is this what you want?Xiangrui Li
Exactly! you did it! thank you! can you please answer my question? however, is it possible to include in your repmat the fact that I don't know the size of my bigger matrix (it can be of any size)?mad

1 Answers

2
votes

Suppose your frames_original and filter_1 only differ at first two dimensions, and we can duplicate filter_1 to match size of frames_original.

m1 = size(frames_original, 1) / size(filter_1, 1);
m2 = size(frames_original, 2) / size(filter_1, 2);
filter_2 = repmat(filter_1, [m1 m2 1]; % error if m1 m2 not integer
filtered = double(frames_original{1}) .* filter_2;