Matlab 中的大小函数和 Python

Size function in Matlab and Python

我正在尝试将此 MATLAB 代码翻译成 Python。这是代码:

function snr_est = estimate_snr(R,r_m,x)

         [L N]=size(R);           % L number of bands (channels)
                                  % N number of pixels (Lines x Columns) 
         [p N]=size(x);           % p number of endmembers (reduced dimension)

         P_y = sum(R(:).^2)/N;
         P_x = sum(x(:).^2)/N + r_m'*r_m;
         snr_est = 10*log10( (P_x - p/L*P_y)/(P_y- P_x) );

return;

我的问题与大小方法有关。对于大小在 MATLAB 代码中的用法,MATLAB 表示 "it returns the number of rows and columns when A is a Matrix"。我想在 Python 中做同样的事情。我知道 a.shape 也 returns 数组的维度。然后可以按照 MATLAB 代码在此函数中要求它们的方式访问它们吗?

MATLAB 的 size 函数和 numpy 的 shape 属性 输出维度相同。

例如,如果 A 是 MATLAB 中的一个 3D 矩阵,它有 3 行,4 列,深度为 5,size(A) 将 return [3, 4, 5].

类似地,在 python 中,如果我有一个具有相同维度的 3D numpy 数组 BB.shape 将 return (3, 4, 5).

在您上面提供的示例中,R 是维度 L x N 的矩阵,x 是大小 p x N 的矩阵。假设您传入 python 函数的矩阵(二维 numpy 数组)相同(即 R.shape=(L,N)x.shape=(p,N),MATLAB 的 size 和 numpy 的 shape将表现相同。