numpy 转存为matlab,“转换” Numpy数组到Matlab,反之亦然

I am looking for a way to pass NumPy arrays to Matlab.

I've managed to do this by storing the array into an image using scipy.misc.imsave and then loading it using imread, but this of course causes the matrix to contain values between 0 and 256 instead of the 'real' values.

Taking the product of this matrix divided by 256, and the maximum value in the original NumPy array gives me the correct matrix, but I feel that this is a bit tedious.

is there a simpler way?

解决方案

Sure, just use scipy.io.savemat

As an example:

import numpy as np

import scipy.io

x = np.linspace(0, 2 * np.pi, 100)

y = np.cos(x)

scipy.io.savemat('test.mat', dict(x=x, y=y))

Similarly, there's scipy.io.loadmat.

You then load this in matlab with load test.

Alteratively, as @JAB suggested, you could just save things to an ascii tab delimited file (e.g. numpy.savetxt). However, you'll be limited to 2 dimensions if you go this route. On the other hand, ascii is the universial exchange format. Pretty much anything will handle a delimited text file.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值