简体   繁体   English

从Python调用MATLAB函数,Python等同于MATLAB'matrix'数据类型吗?

[英]calling MATLAB function from Python, what is Python's equivalent of MATLAB 'matrix' datatype?

This question relates to the situation where only MATLAB runtime is present - the computer does not have a full MATLAB license. 这个问题与仅存在MATLAB运行时的情况有关-计算机没有完整的MATLAB许可证。

A MATLAB function has been compiled for use by Python as described here : 一个MATLAB功能已编制供Python作为描述在这里

These instructions have been used successfully to call a user defined MATLAB function from Python and get a return value where parameters are int or float . 这些指令已成功用于从Python调用用户定义的MATLAB函数并获得参数为intfloat的返回值。 However, if I call a user defined MATLAB function f(matrixParam) which has a parameter of MATLAB type matrix then no matter what data type I use in Python for it, I get wrong type errors. 但是,如果我调用一个用户定义的MATLAB函数f(matrixParam) ,该函数具有MATLAB类型matrix的参数,那么无论我在Python中使用哪种数据类型,都会出现错误的类型错误。 I tried the following Python types: list of lists ( [[1,2],[3,4]] ), Pandas.DataFrame and NumPy.matrix - nothing works. 我尝试了以下Python类型:列表列表[[1,2],[3,4]] ),Pandas.DataFrame和NumPy.matrix-无效。 In MATLAB a variable a of type matrix may be defined like this: a = [1 2 3; 4 5 6; 7 8 10] 在MATLAB中,可以这样定义matrix类型的变量aa = [1 2 3; 4 5 6; 7 8 10] a = [1 2 3; 4 5 6; 7 8 10] a = [1 2 3; 4 5 6; 7 8 10] . a = [1 2 3; 4 5 6; 7 8 10]

Would anyone know how I would define in Python a variable that would be acceptable by a MATLAB function as being of type matrix ? 有谁知道我将如何在Python中定义一个变量,该变量将被MATLAB函数接受为matrix类型?

The answer turned out to be: 答案是:

import matlab

myMatrix = matlab.double([[1,2],[3,4]])

myMatrix can then be passed to a MATLAB function. 然后可以将myMatrix传递给MATLAB函数。 MATLAB will even get True if it tests its type: ismatrix(myMatrix) . 如果MATLAB测试其类型: ismatrix(myMatrix)甚至会得到True。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM