www.gusucode.com > signal 案例源码程序 matlab代码 > signal/OutputMatrixSizeExample.m

    %% Output Matrix Size and Element Computation

%%
% Create two matrices, |M1| and |M2|.

M1 = [17 24  1  8 15;
      23  5  7 14 16;
       4  6 13 20 22;
      10 12 19 21  3;
      11 18 25  2  9];

M2 = [8 1 6;
      3 5 7;
      4 9 2];

%%
% |M1| is 5-by-5 and |M2| is 3-by-3, so their cross-correlation has size
% (5+3-1)-by-(5+3-1), or 7-by-7. In terms of lags, the resulting matrix is
%
% $$C = \pmatrix{
% c_{-2,-2}&c_{-2,-1}&c_{-2,0}&c_{-2,1}&c_{-2,2}&c_{-2,3}&c_{-2,4}\cr
% c_{-1,-2}&c_{-1,-1}&c_{-1,0}&c_{-1,1}&c_{-1,2}&c_{-1,3}&c_{-1,4}\cr
% c_{ 0,-2}&c_{ 0,-1}&c_{ 0,0}&c_{ 0,1}&c_{ 0,2}&c_{ 0,3}&c_{ 0,4}\cr
% c_{ 1,-2}&c_{ 1,-1}&c_{ 1,0}&c_{ 1,1}&c_{ 1,2}&c_{ 1,3}&c_{ 1,4}\cr
% c_{ 2,-2}&c_{ 2,-1}&c_{ 2,0}&c_{ 2,1}&c_{ 2,2}&c_{ 2,3}&c_{ 2,4}\cr
% c_{ 3,-2}&c_{ 3,-1}&c_{ 3,0}&c_{ 3,1}&c_{ 3,2}&c_{ 3,3}&c_{ 3,4}\cr
% c_{ 4,-2}&c_{ 4,-1}&c_{ 4,0}&c_{ 4,1}&c_{ 4,2}&c_{ 4,3}&c_{ 4,4}\cr}.$$
%%
% As an example, compute the element $c_{0,2}$ (or |C(3,5)| in MATLAB(R),
% since |M2| is 3-by-3). Line up the two matrices so their |(1,1)| elements
% coincide. This placement corresponds to $c_{0,0}$. To find $c_{0,2}$,
% slide |M2| two rows to the right.
%
% <<../xcorr2ex.png>>

%%
% Now |M2| is on top of the matrix |M1(1:3,3:5)|. Compute the
% element-by-element products and sum them. The answer should be
%
% $$1\times8+7\times3+13\times4+8\times1+14\times5+20\times9+15
% \times6+16\times7+22\times2=585.$$

[r2,c2] = size(M2);

CC = sum(sum(M1(0+(1:r2),2+(1:c2)).*M2))

%%
% Verify the result using |xcorr2|.

D = xcorr2(M1,M2);

DD = D(0+r2,2+c2)