代码之家  ›  专栏  ›  技术社区  ›  Vlad Vadean

Matlab数组乘法

  •  -1
  • Vlad Vadean  · 技术社区  · 1 年前

    两者之间有什么区别 * .* 在Matlab中?

    1 回复  |  直到 1 年前
        1
  •  20
  •   Nick    13 年前

    * 是向量或矩阵乘法 .* 是元素乘法

    a = [ 1; 2]; % column vector
    b = [ 3 4]; % row vector
    
    a*b
    
    ans =
    
         3     4
         6     8
    

    虽然

    a.*b.' % .' means tranpose
    
    ans =
    
         3
         8
    
        2
  •  8
  •   Dan fatihk    13 年前

    * 是矩阵乘法,而 .* 是元素乘法。

    为了使用第一个运算符,操作数在大小方面应遵守矩阵乘法规则。

    对于第二个运算符,向量长度(垂直或水平方向可能不同)或矩阵大小应相等,以便进行元素乘法

        3
  •  0
  •   Ron at BiophysicsLab    4 年前

    * 是矩阵乘法,而 .* 是元素数组乘法

    我创建了这个简短的脚本,以帮助澄清关于这两种乘法形式的挥之不去的问题。。。

    %% Difference between * and .* in MatLab
    
    % * is matrix multiplication following rules of linear algebra
    % See MATLAB function mtimes() for help
    
    % .* is Element-wise multiplication follow rules for array operations
    % Also called: Hadamard Product, Schur Product and broadcast
    % mutliplication
    % See MATLAB function times() for help
    
    % Given: (M x N) * (P x Q)
    % For matrix multiplicaiton N must equal P and output would be (M x Q)
    % 
    % For element-wise array multipication the size of each array must be the
    % same, or be compatible. Where compatible could be a scalar combined with
    % each element of the other array, or a vector with different orientation
    % that can expand to form a matrix.
    
    a = [ 1; 2] % column vector
    b = [ 3 4] % row vector
    
    disp('matrix multiplication: a*b')
    a*b
    disp('Element-wise multiplicaiton: a.*b')
    a.*b
    
    c = [1 2 3; 1 2 3]
    d = [2 4 6]
    
    disp("matrix multiplication (3 X 2) * (3 X 1): c*d'")
     c*d'
    disp('Element-wise multiplicaiton (3 X 2) .* (1 X 3): c.*d')
    c.*d
    
    % References: 
    % https://www.mathworks.com/help/matlab/ref/times.html
    % https://www.mathworks.com/help/matlab/ref/mtimes.html
    % https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html
    

    脚本结果:

     1
     2
    

    b

     3     4
    

    矩阵乘法:a*b

    ans=

     3     4
     6     8
    

    元素乘法:a.*b

    ans=

    3     4
    6     8
    

    c

     1     2     3
     1     2     3
    

    d

     2     4     6
    

    矩阵乘法(3×2)*(3×1):c*d'

    ans=

    28
    28
    

    元素乘法(3 X 2)。*(1 X 3):c.*d

    ans=

     2     8    18
     2     8    18