matlab输入n维数据需要什么格式?
我有一个用Python脚本为我正在进行的数据挖掘项目制作的4维字典,我想将数据读入Matlab以对数据进行一些统计测试。
读取二维矩阵很简单。我想,由于我的第一个维度只有 4 深,我可以将其每个切片写入一个单独的文件(总共 4 个文件),每个文件都有许多二维切片,看起来像这样:
2 3 6
4 5 8
6 7 3
1 4 3
6 6 7
8 9 0
然而,这并不工作,matlab 将其读取为单个连续的 6 x 3 矩阵。我什至看了一下 dlmread,但不知道如何让它做我想做的事。如何格式化它以便我可以将 3 个(或最好更多)维度放入一个文件中?
I have a 4-dimensional dictionary I made with a Python script for a data mining project I'm working on, and I want to read the data into Matlab to do some statistical tests on the data.
To read a 2-dimensional matrix is trivial. I figured that since my first dimension is only 4-deep, I could just write each slice of it out to a separate file (4 files total) with each file having many 2-dimensional slices, looking something like this:
2 3 6
4 5 8
6 7 3
1 4 3
6 6 7
8 9 0
This however does not work, and matlab reads it as a single continuous 6 x 3 matrix. I even took a look a dlmread but could not figure out how to get it do what I wanted. How do I format this so I can put 3 (or preferably more) dimensions in a single file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个简单的解决方案是创建一个仅包含两行的文件:第一行包含目标数组大小,第二行包含所有数据。然后,您需要做的就是重塑数据。
假设您的文件是
您执行以下操作将数组读入变量
data
查看
data
以了解 Matlab 如何对多维数组中的元素进行排序。A simple solution is to create a file with two lines only: the first line contains the target array size, the second line contains all your data. Then, all you need to do is reshape the data.
Say your file is
You do the following to read the array into the variable
data
Have a look at
data
to see how Matlab orders elements in multidimensional arrays.Matlab 按列存储数据。因此,从您的示例中(假设它是一个 3x2x3 矩阵),matlab 会将其存储为第一个“切片”中的第一、第二和第三列,然后是第二个切片中的第一、第二个第三列,依此类推,
所以您可以像这样从 python 中写出数据(我不知道如何),然后将其读入 matlab。然后,您可以将其
重塑
为 3x2x3 矩阵,并且您将保留正确的顺序。Matlab stores data column wise. So from your example (assuming its a 3x2x3 matrix), matlab will store it as first, second and third column from the first "slice", followed by the first, second third columns from the second slice and so on like this
So you can write the data out like this from python (I don't know how) and then read it into matlab. Then you can
reshape
it back into a 3x2x3 matrix and you'll retain your correct ordering.