Display Python Formatted Arrays

版本 1.3.0.0 (1.6 KB) 作者: Robin
Displays a double array in Python/NumPy format
1.1K 次下载
更新时间 2010/3/13

查看许可证

This class inherits from double and overloads the display function to output multidimensional arrays in text format that is compatible for copy and pasting into an interactive Python session.

It doesn't quite work with arbitrary dimensions, since I got stuck with generalised indexing, hence the hackish:
newx = squeeze(x(i,:,:,:,:,:,:,:,:,:,:));
(I'd be interested to hear if theres a different way to do this!)
So at the moment it supports about 12 dimensions, which I thought was probably enough for most copy + paste needs, but if you need more you can just add more colons to the line above - it doesn't seem to do any harm!

Anyway with the file in your Matlab path:
>> x = rand(2,3,4,5);
>> a = array(x)

You can specify a fprintf style format string either in the
constructor or after:
>> a = array(x,'%2.6f')
>> a.format = '%2.2f'

eg:
>> x = rand(4,3,2);
>> array(x)
ans =

array([[[2.071566461449581e-01, 3.501602151029837e-02],
[1.589135260727248e-01, 3.766891927380323e-01],
[8.757206127846399e-01, 7.259276565938600e-01]],

[[7.570839415557700e-01, 3.974969411279816e-02],
[8.109207856487061e-01, 5.043242527988604e-01],
[6.351863794630047e-01, 7.013280585980169e-01]],

[[8.863281096304466e-01, 9.885678912262633e-01],
[4.765077527169480e-01, 7.634956792870943e-01],
[9.728134909163066e-02, 4.588908258125032e-01]],

[[4.722298594969571e-01, 6.861815984603373e-01],
[1.162875322461844e-01, 4.887479677951201e-02],
[9.084394562396312e-01, 5.822948089552498e-01]]])

引用格式

Robin (2024). Display Python Formatted Arrays (https://www.mathworks.com/matlabcentral/fileexchange/24087-display-python-formatted-arrays), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2008b
兼容任何版本
平台兼容性
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.3.0.0

another silly bug - missing off first element...

1.2.0.0

Update file - fix problem with column array

1.1.0.0

More detailed description with an example.

1.0.0.0