在MATLAB中,很容易找到满足特定条件的值的索引:
>> a = [1,2,3,1,2,3,1,2,3];
>> find(a > 2) % find the indecies where this condition is true
[3, 6, 9] % (MATLAB uses 1-based indexing)
>> a(find(a > 2)) % get the values at those locations
[3, 3, 3]
用Python做这件事最好的方法是什么?
到目前为止,我提出了以下建议。
从C接口来看,Python与PyErr_Print()的等价性是什么?
我假设在sys或traceback模块中调用,但在其中找不到调用PyErr_Print()的任何函数。
附录
我在进行Python调用,以获得与PyErr_PrintEx()相同的功能,描述为:
Print a standard traceback to sys.stderr and clear the error indicator.
也就是说,我想让Python调用具有这种效果。
我在一个虚拟环境中运行Python2.7和matlab2015a。matlab和python都在路径中(我可以从终端启动它们)。但是,每当我这样做的时候:
python
from matlab.engine import pythonengine
pythonengine.find_matlab()
我明白了:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no att
我正在将Python类翻译到Matlab。其中大部分都是简单明了的,但我对Python语法不太熟悉(我很少使用它)。我被困在以下几个方面:
# find the basis that will be uncorrelated using the covariance matrix
basis = (sqrt(eigenvalues)[newaxis,:] * eigenvectors).transpose()
有人能帮我弄清楚Matlab的语法是什么吗?
我在谷歌上发现,np.newaxis增加了数组的维数,而transpose非常清楚。因此,对于newaxis来说,在matlab中使用cat
我正在尝试离开matlab,转而使用python + matplotlib。然而,我还没有真正弄清楚matplotlib中的matplotlib中的“句柄”是什么。下面是一些matlab代码,我在其中返回句柄,以便更改某些属性。与使用matplotlib的代码完全等效的是什么?我经常在matlab中使用句柄的'Tag‘属性,并对它使用'findobj’。这也可以用matplotlib来完成吗?
% create figure and return figure handle
h = figure();
% add a plot and tag it so we can find
在我的Matlab代码中有一条声明:
A=查找(abs(ASE_lamda-YDFA_lam)<1e-15);
执行后,输出如下:
octave:50> whos a
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
a 1x1 8 double
有什么方法可以用NumPy来为剩余的维度使用平面索引吗?我正在尝试将下面的MATLAB函数转换为Python
function [indices, weights] = locate(values, gridpoints)
indices = ones(size(values));
weights = zeros([2, size(values)]);
for ix = 1:numel(values)
if values(ix) <= gridpoints(1)
indices(ix) = 1;
w
我需要从python运行一个matlab脚本。matlab脚本返回一个数组,我猜它可以保存为*.mat文件。
有没有可能直接从python中执行matlab代码,然后检索它?
例如:
myarr = somematlabbridge.execute("matlab function")
或者,有没有可能这样做:
somematlabbridge.execute("matlab functions to execute the matlab and save it in a file")
这样我就可以将文件读取到Python中,以检索matlab函数的输出?
我需