Convert matlab zeros function to Python equivalent

22 次查看(过去 30 天)
Hi Could you pleas elet me know what would be a similar conversion of matlab zeros to np.zeros for datatype. I am getting an error while doing this.
Matlab code:
Shifts = zeros(length(Filters)-1,1,'int16');
Python code:
Shifts = np.zeros(len(Filters)-1,1,dtype = np.int16)
  6 个评论
Prb
Prb 2019-5-29
It took only the negative dimensions and which is not allowed.
Murugan C
Murugan C 2019-5-29
What is size of the Filters. If lenght of "Filters" is empty or zeros, then only you will get negative dimensitons.
>>> import numpy
>>> Filters = [ ]
>>> Shifts = numpy.zeros(len(Filters)-1,dtype = numpy.int16)
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
Shifts = numpy.zeros(len(Filters)-1,dtype = numpy.int16)
ValueError: negative dimensions are not allowed
>>>
%%
>>> Filters = [5, 6, 4, 5,6]
>>> Shifts = numpy.zeros(len(Filters)-1,dtype = numpy.int16)
>>> Shifts
array([0, 0, 0, 0], dtype=int16)

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2019-5-29
编辑:Stephen23 2019-5-29
You need to read the numpy zeros documentation, because your syntax does not actually match its specification:
import numpy as np
Filters = [1,2,3];
Shifts = np.zeros((len(Filters)-1,1),dtype=np.int16)
% ^ ^ The shape needs to be ONE iterable!
Most likely you do not need a 2D numpy array, and a simple 1D array would suffice:
Shifts = np.zeros(len(Filters)-1,dtype=np.int16)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by