什么格式那样的matlab需要n维数据输入?格式、数据、matlab

由网友(暖音小哥)分享简介:我有一个4维的词典我做了一个数据挖掘项目我工作的一个Python脚本,我想读取数据到MATLAB做一些统计检验上的数据。I have a 4-dimensional dictionary I made with a Python script for a data mining project I'm working...

我有一个4维的词典我做了一个数据挖掘项目我工作的一个Python脚本,我想读取数据到MATLAB做一些统计检验上的数据。

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.

要读取一个二维矩阵是微不足道的。我想,既然我的第一个维度是只有4级深,我可以只写它的每片出到一个单独的文件(4个文件总计)具有许多2维片的每个文件,看起来像这样:

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

然而,这并不工作,MATLAB把它读成一个单一的连续6×3矩阵。我甚至看了看dlmread但无法弄清楚如何得到它做什么,我想要的。我如何格式化这让我可以把3(或preferably更多)尺寸在一个文件?

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?

推荐答案

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

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.

假设你的文件

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

看一看数据看到多维数组如何Matlab的订单元素。

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

阅读全文

相关推荐

最新文章