numpy基础——matrix.transpose() 和 matrix.getA()

本文介绍了numpy库中matrix对象的getA方法,它将matrix转换为ndarray对象,等价于np.asarray(self)。同时讨论了transpose函数,用于进行矩阵转置,1-D数组不受影响,2-D数组则是常规的转置操作,对于n-D数组,可以指定轴的排列方式。附带了相关示例。

numpy.matrix.getA

matrix.getA()[source]

返回一个数组对象

Return self as an ndarray object.

Equivalent to np.asarray(self).

Parameters: None
Returns: ret : ndarray
self as an ndarray

Examples

>>> x = np.matrix(np.arange(12).reshape((3,4))); x
matrix([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
>>> x.getA()
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

numpy.matrix.transpose

矩阵转置

Returns a view of the array with axes transposed.

For a 1-D array, this has no effect. (To change between column and row vectors, first cast the 1-D array into a matrix object.) For a 2-D array, this is the usual matrix transpose. For an n-D array, if axes are given, their order indicates how the axes are permuted (see Examples). If axes are not provided and a.shape = (i[0], i[1], … i[n-2], i[n-1]), then a.transpose().shape = (i[n-1], i[n-2], … i[1], i[0]).

Examples

>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a.transpose()
array([[1, 3],
       [2, 4]])
>>> a.transpose((1, 0))
array([[1, 3],
       [2, 4]])
>>> a.transpose(1, 0)
array([[1, 3],
       [2, 4]])
def mat_conv(a, b, zi = None): """ Convolutional matrix multiplication, in which matrix cells are sequences instead of numbers, and element-wise multiplication becomes convolution. Given A[I][J][Ta], B[J][K][Tb], output C[I][K][Tc], where Tc = Ta + Tb - 1 and C[i][k] = sum_j{ A[i][j] * B[j][k]}, where * is the colvolution operator. In streaming overlap-add mode (activated by specifying a nontrivial zi param) the output returned in two parts: part 1 is same length as *a* in time, part 2 is the new zi, which is OLA-ed onto the next output. A, B shall be dimension 2 or 3. Dimensions i, j, k must be present, while dimension t can be omitted if T=1 (in which case mat_conv reduces to numpy dot). Parameters ---------- a, b : arrays (2d or 3d) The multiplicant matrices, where the matrix cells are numbers (2d) or sequences. zi : array, optional The delay line (state) used for streaming OLA. Unused (but may be returned) if either multiplicant is unit length in time. """ if a.ndim == 2: if b.ndim == 2: return a.dot(b) if zi is None else (a.dot(b), zi) return b.T.dot(a.T).T if zi is None else (b.T.dot(a.T).T, zi) if b.ndim == 2: result = a.transpose(2, 0, 1).dot(b).transpose(1, 2, 0) return result if zi is None else (result, zi) if b.shape[-1] == 1: result = a.transpose(2, 0, 1).dot(b[:,:,0]).transpose(1, 2, 0) return result if zi is None else (result, zi) I, J, m = a.shape J, K, n = b.shape c = np.zeros((I, K, m + n - 1)) for i in range(I): for k in range(K): for j in range(J): c[i, k, :] += np.convolve(a[i,j,:], b[j,k,:]) if zi is None: return c else: c[:, :, :zi.shape[-1]] += zi return c[:, :, :m], c[:, :, m:] 这个函数功能,mic_in = [mat_conv(marray.get_responses(sc.azimuth, sc.colatitude, fs=fs), sc.waves[:, None])[:, 0] for sc in scenes] # [sc][mic, t]还有这个用法
最新发布
12-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值