matlab语言转化为python语言_将Matlab代码转换为python(scipy)

1586010002-jmsa.png

I' trying to translate this matlab code to python:

T = length(z);

lambda = 10;

I = speye(T)

D2 = spdiags(ones(T-2,1)*[1 -2 1],[0:2],T-2,T);

z_stat = (I-inv(I + lambda^2*D2'*D2))*z;

What I got at the moment:

T = len(signal)

lam = 10;

I = np.identity(T)

D2 = scipy.sparse.spdiags(np.ones((T-2,1),dtype=np.int)*[1,-2,1],(range(0,3)),T-2,T);

At the moment I get this error

"scipy.sparse.sp...ge(0,3)),T-2,T)" ValueError: number of diagonals

(298) does not match the number of offsets (3) args tuple: ('number

of diagonals (298) does not match the number of offsets (3)',)

When looking at the documentation, the matlab function and the python function are very similar. Though there is probably a difference which I am missing. My question is now: What am I doing wrong ?

edit: z is an array with length 300

解决方案

If you transpose the filter data to spdiags then you get an answer of the same dimensions in both packages:

# numpy/scipy

filt = [1,-2,1]* np.ones((1,T-2),dtype=np.int).T

D2 = scipy.sparse.spdiags(data.T, (range(0,3)),T-2,T)

np.shape(D2)

>>> (298, 300)

% matlab check

D2 = spdiags(ones(T-2,1)*[1 -2 1],[0:2],T-2,T)

size(D2)

ans =

298 300

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值