gpt4 book ai didi

python - 使用 oct2py 进行矩阵乘法

转载 作者:行者123 更新时间:2023-12-01 05:26:53 24 4
gpt4 key购买 nike

我正在使用 oct2py 通过 python 运行 Octave 函数。它正在工作,但当我尝试乘以 2 个矩阵时出现错误。我该怎么做才能解决这个问题?

这是一个示例 matlab 函数

%% MATLAB
function lol = jk2(arg1,arg2)
arg1 = arg1;
arg2 = arg2;
lol = arg1*arg2;

end

这是调用函数的代码

import oct2py
from oct2py import octave
a=3
b=4
octave.call("/MATLAB/jk2.m",a,b) # this call works
a=np.array([[1,2],[3,4]])
b=np.array([[5,6],[1,2]])
octave.call("/MATLAB/jk2.m",a,b) # this call report an errors

这是错误消息

>>> octave.call("/home/donbeo/Documents/MATLAB/jk2.m",a,b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/oct2py-1.2.0-py2.7.egg/oct2py/session.py", line 210, in call
resp = self._eval(cmd, verbose=verbose)
File "/usr/local/lib/python2.7/dist-packages/oct2py-1.2.0-py2.7.egg/oct2py/session.py", line 350, in _eval
return self._session.evaluate(cmds, verbose, log, self.logger)
File "/usr/local/lib/python2.7/dist-packages/oct2py-1.2.0-py2.7.egg/oct2py/session.py", line 523, in evaluate
raise Oct2PyError(msg)
oct2py.utils.Oct2PyError: Oct2Py tried to run:
"""
[a__] = jk2(A__, B__)
"""
Octave returned:
binary operator '*' not implemented for 'int64 matrix' by 'int64 matrix' operations
>>>

最佳答案

这是一个 Python 和 Octave 之间的界限变得模糊的示例。 Numpy 将数组解释为整数类型(因为没有显式 float ),但 Octave 会将数组视为 double 。如果您在数组定义中的任何位置添加句点,它都会起作用。

修复(测试)示例:

from oct2py import octave
import numpy as np
a = np.array([[1, 2], [3, 4.]]) # notice the addition of the period
b = np.array([[5, 6], [1, 2], dtype=float]) # another way to specify floating point type
octave.call("/MATLAB/jk2.m", a, b) # this call works just fine

关于python - 使用 oct2py 进行矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21192714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com