matlab输入n维数据需要什么格式?

发布于 2024-11-15 16:21:47 字数 327 浏览 0 评论 0原文

我有一个用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆里有你的影子 2024-11-22 16:21:47

一个简单的解决方案是创建一个仅包含两行的文件:第一行包含目标数组大小,第二行包含所有数据。然后,您需要做的就是重塑数据。

假设您的文件是

3 2 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

您执行以下操作将数组读入变量 data

fid = fopen('myFile'); %# open the file (don't forget the extension)
arraySize = str2num(fgetl(fid)); %# read the first line, convert to numbers
data = str2num(fgetl(fid)); %# read the second line
data = reshape(data,arraySize); %# reshape the data
fclose(fid); %# close the file

查看 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

3 2 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

You do the following to read the array into the variable data

fid = fopen('myFile'); %# open the file (don't forget the extension)
arraySize = str2num(fgetl(fid)); %# read the first line, convert to numbers
data = str2num(fgetl(fid)); %# read the second line
data = reshape(data,arraySize); %# reshape the data
fclose(fid); %# close the file

Have a look at data to see how Matlab orders elements in multidimensional arrays.

薄情伤 2024-11-22 16:21:47

Matlab 按列存储数据。因此,从您的示例中(假设它是一个 3x2x3 矩阵),matlab 会将其存储为第一个“切片”中的第一、第二和第三列,然后是第二个切片中的第一、第二个第三列,依此类推,

2 
4
3
5 
6
8
6
1
7
4 
3
3
6
8
6
9
7
0

所以您可以像这样从 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

2 
4
3
5 
6
8
6
1
7
4 
3
3
6
8
6
9
7
0

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文