python 空矩阵,在Python中初始化空矩阵

I am trying to convert a MATLAB code in Python. I don't know how to initialize empty matrix in Python.

MATLAB Code:

demod4(1) = [];

I tried in Python

demod4[0] = array([])

but it gives error:

only length-1 arrays can be converted to Python scalars

解决方案

If you are using numpy arrays, you initialize to 0, by specifying the expected matrix size:

import numpy as np

d = np.zeros((2,3))

>>> d

[[ 0. 0. 0.]

[ 0. 0. 0.]]

This would be the equivalent of MATLAB 's:

d = zeros(2,3);

You can also initialize an empty array, again using the expected dimensions/size

d = np.empty((2,3))

If you are not using numpy, the closest somewhat equivalent to MATLAB's d = [] (i.e., a zero-size matrix) would be using an empty list and then

append values (for filling a vector)

d = []

d.append(0)

d.append(1)

>>> d

[0, 1]

or append lists (for filling a matrix row or column):

d = []

d.append(range(0,2))

d.append(range(2,4))

>>> d

[[0, 1], [2, 3]]

See also:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值