Calling Matlab function from python: “initializer must be a rectangular nested sequence”
2024-10-18 23:29:42
I am writing a python script from which I hope to call the Matlab anovan function. I have attempted a basic test of this feature on file test.txt.
This file is imported as array data
. In matlab, the anovan function would be called as follows:
anovan(data(:,1),{data(:,2),data(:,3)})
In python, I have attempted to call the function like this:
import matlab.engine
eng = matlab.engine.start_matlab()
data = np.genfromtxt('test.txt')
eng.anovan(matlab.double(data[:,0]),matlab.int8(data[:,1:2]))
Unfortunately, this gives the following error:
ValueError: initializer must be a rectangular nested sequence
I can see that there is some issue in passing the last two array columns to the function, but am unsure of the solution. Any suggestions would be appreciated.
I have now worked this out. The issue was in the format of the data passed to the function.
#convert grouping columns to integer lists
list1 = data[:,1].tolist()
for i in range(0,len(list1)):
list1[i] = int(list1[i])
list2 = data[:,2].tolist()
for i in range(0,len(list2)):
list2[i] = int(list2[i])
#The following command now works
eng.anovan(matlab.double(data[:,0].tolist()),[matlab.int8(list1),matlab.int8(list2)])
最新文章
- UVA 11768 Lattice Point or Not(扩展欧几里德)
- 我所了解的各公司使用的 Ceph 存储集群 (携程、乐视、有云、宝德云、联通等)
- 将一个正整数分解为m个2的n次方的和
- BZOJ4662 : Snow
- HDU 2064 (递推) 汉诺塔III
- linux 体系结构知识 博客
- POJ 1276 (多重背包) Cash Machine
- JAVA 正则表达式 (超详细)
- CentOS内核升级
- 对称密码-分组密码-AES
- #UnityTips# 2017.11.14
- GraphicsMagick的命令行使用示例
- PTA中提交Python3程序的一些套路
- UOJ182 a^-1 + b problem 解题报告
- Maven入门教程(一)
- [05-02]红帽linux常用操作命令
- Java OPC 代码
- Codeforces 387E George and Cards
- hdu 1043 Eight (八数码问题)【BFS】+【康拓展开】
- python实现json转excel