如何按以下方式操作数据?

发布于 2024-11-18 02:26:03 字数 2734 浏览 1 评论 0原文

我有一个名为 data.dat 的文件,其内容(示例):

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F

2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6

3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F

2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6

3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

.
.
.

我试图将每 3 行数组合并到最后的一个行数组中,以给出一个 fx 29 矩阵code>:

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

.
.
.

然后将第 10 列和第 11 列移动到第 1 行和第 2 行:

E F 0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

E F 0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

如何在 MATLAB 中执行此操作?这是我的尝试,但它是不正确的。

% Find out number of rows in file

rline=0;
x=0;

% Open Data File
fid = fopen('data.dat','rt');

% Loop through data file until we get a -1 indicating EOF
while(x~=(-1))
  x=fgetl(fid);
  rline=rline+1;
end
rline = rline-1;

% How many row in final file

fline=rline/3; % one row in final file represent by 3 rows from raw data

% Create 3 seperate matrix named as z1,z2,z3

frewind(fid);
for i = 1:rline
  num1 = fscanf(fid,'%f %f %f %f %f %f %f %f %f\n')'; % Read in numbers
  name1 = fscanf(fid,'%s %s',rline); % Filter out string at end of line
  if(i==1)
    result1 = num1; % Add 1st row
    names1 = name1; % Add 1st text string
  else
    result1 = [result1;num1]; % Add additional rows
    names1 = char(names1,name1); % Add next string
    names1 = names1';
  end
  i=i+3;
end

fclose(fid);

z1 = result1;
zname= names1;

frewind(fid);
for i = 2:rline
  num2 = fscanf(fid,'%f')'; % Read in numbers

  if(i==2)
    result2 = num2; % Add 2nd row
  else
    result2 = [result2;num2]; % Add additional rows    
  end
  i=i+3;
end

fclose(fid);

z2 = result2;

frewind(fid);
for i = 3:rline
  num3 = fscanf(fid,'%f')'; % Read in numbers    
  if(i==3)
    result3 = num3; % Add 3rd row    
  else
    result3 = [result3;num3]; % Add additional rows

  end
  i=i+3;
end

fclose(fid);

z3 = result3;

% Create a final data matrix of F = (fline x 29)

for i = 1: fline
    for j = 1: fline
        F(i, [1:2]) = zname(j,:);
        F(i, [3:11]) = z1(j,:);
        F(i, [12:20]) = z2(j,:);
        F(i, [21:29]) = z3(j,:);
        j=j+1;
    end
    i=i+1;
end

Final_data = [F];

I have a file named data.dat with contents (sample):

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F

2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6

3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F

2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6

3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

.
.
.

I'm trying to merge every 3 rows array into one row array at the end to give a matrix of f x 29:

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

.
.
.

and then shift the 10th and 11th column to the 1st and 2nd row:

E F 0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

E F 0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6 3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

How do I do this in MATLAB? Here is my attempt, but it is incorrect.

% Find out number of rows in file

rline=0;
x=0;

% Open Data File
fid = fopen('data.dat','rt');

% Loop through data file until we get a -1 indicating EOF
while(x~=(-1))
  x=fgetl(fid);
  rline=rline+1;
end
rline = rline-1;

% How many row in final file

fline=rline/3; % one row in final file represent by 3 rows from raw data

% Create 3 seperate matrix named as z1,z2,z3

frewind(fid);
for i = 1:rline
  num1 = fscanf(fid,'%f %f %f %f %f %f %f %f %f\n')'; % Read in numbers
  name1 = fscanf(fid,'%s %s',rline); % Filter out string at end of line
  if(i==1)
    result1 = num1; % Add 1st row
    names1 = name1; % Add 1st text string
  else
    result1 = [result1;num1]; % Add additional rows
    names1 = char(names1,name1); % Add next string
    names1 = names1';
  end
  i=i+3;
end

fclose(fid);

z1 = result1;
zname= names1;

frewind(fid);
for i = 2:rline
  num2 = fscanf(fid,'%f')'; % Read in numbers

  if(i==2)
    result2 = num2; % Add 2nd row
  else
    result2 = [result2;num2]; % Add additional rows    
  end
  i=i+3;
end

fclose(fid);

z2 = result2;

frewind(fid);
for i = 3:rline
  num3 = fscanf(fid,'%f')'; % Read in numbers    
  if(i==3)
    result3 = num3; % Add 3rd row    
  else
    result3 = [result3;num3]; % Add additional rows

  end
  i=i+3;
end

fclose(fid);

z3 = result3;

% Create a final data matrix of F = (fline x 29)

for i = 1: fline
    for j = 1: fline
        F(i, [1:2]) = zname(j,:);
        F(i, [3:11]) = z1(j,:);
        F(i, [12:20]) = z2(j,:);
        F(i, [21:29]) = z3(j,:);
        j=j+1;
    end
    i=i+1;
end

Final_data = [F];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

半城柳色半声笛 2024-11-25 02:26:03

我正在使用您提供的示例数据文件作为示例。 TEXTSCAN 函数用于解析文件

data.dat

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F
2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6
3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1
0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F
2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6
3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

MATLAB

%# parse data file
fid = fopen('data.dat','rt');
C = textscan(fid, [repmat('%f ',[1 9]) '%s %s'], 'CollectOutput',true);
fclose(fid);

%# extract and reshape numeric data
M = C{1};
M = reshape(M', size(M,2)*3, [])';   %# similar to 'Michael J. Barber' answer

%# extract textual data
T = C{2}(1:3:end,:);

%# we can merge all into one cell array
data = [T num2cell(M)];

代码由于数据包含异构类型(数字和字符),因此我们单独读取和存储它们。最后一行代码显示了将所有数据合并到单个元胞数组中的一种方法:

data = 
    'E'    'F'    [0]    [2.3000]    [4.5000]    [0.9000]    [0.5000]    [3.4000]    [0]    [0.3000]    [0.5000]    [2.9000]    [5.4000]    [7.2000]    [4.8000]    [3.7000]    [9.1000]    [2.3000]    [4.1000]    [5.6000]    [3.4000]    [6.1000]    [4.8000]    [6.4000]    [0.4000]    [0.6000]    [0.3000]    [5.4000]    [7.1000]
    'E'    'F'    [0]    [2.3000]    [4.5000]    [0.9000]    [0.5000]    [3.4000]    [0]    [0.3000]    [0.5000]    [2.9000]    [5.4000]    [7.2000]    [4.8000]    [3.7000]    [9.1000]    [2.3000]    [4.1000]    [5.6000]    [3.4000]    [6.1000]    [4.8000]    [6.4000]    [0.4000]    [0.6000]    [0.3000]    [5.4000]    [7.1000]

I am using the sample data file you've given as an example. The TEXTSCAN function is used to parse the file

data.dat

0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F
2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6
3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1
0.0 2.3 4.5 0.9 0.5 3.4 0.0 0.3 0.5 E F
2.9 5.4 7.2 4.8 3.7 9.1 2.3 4.1 5.6
3.4 6.1 4.8 6.4 0.4 0.6 0.3 5.4 7.1

MATLAB code

%# parse data file
fid = fopen('data.dat','rt');
C = textscan(fid, [repmat('%f ',[1 9]) '%s %s'], 'CollectOutput',true);
fclose(fid);

%# extract and reshape numeric data
M = C{1};
M = reshape(M', size(M,2)*3, [])';   %# similar to 'Michael J. Barber' answer

%# extract textual data
T = C{2}(1:3:end,:);

%# we can merge all into one cell array
data = [T num2cell(M)];

Note that since the data contains heterogeneous types (numeric and characters), we read and store them separately. The final line of code shows one way of merging all data into a single cell array:

data = 
    'E'    'F'    [0]    [2.3000]    [4.5000]    [0.9000]    [0.5000]    [3.4000]    [0]    [0.3000]    [0.5000]    [2.9000]    [5.4000]    [7.2000]    [4.8000]    [3.7000]    [9.1000]    [2.3000]    [4.1000]    [5.6000]    [3.4000]    [6.1000]    [4.8000]    [6.4000]    [0.4000]    [0.6000]    [0.3000]    [5.4000]    [7.1000]
    'E'    'F'    [0]    [2.3000]    [4.5000]    [0.9000]    [0.5000]    [3.4000]    [0]    [0.3000]    [0.5000]    [2.9000]    [5.4000]    [7.2000]    [4.8000]    [3.7000]    [9.1000]    [2.3000]    [4.1000]    [5.6000]    [3.4000]    [6.1000]    [4.8000]    [6.4000]    [0.4000]    [0.6000]    [0.3000]    [5.4000]    [7.1000]
很糊涂小朋友 2024-11-25 02:26:03

您可以使用 Matlab 相当轻松地操作数组,无需循环。我将使用较小的数组来更容易地看到发生了什么。我也会使用 Octave,但在这里没有什么区别。我还将假设矩阵已经在变量中可用(使用 load 来执行此操作,它比问题中的方法容易得多)。

首先,考虑一个矩阵 A

octave-3.0.0:23> A
A =

    1    7
    2    8
    3    9
    4   10
    5   11
    6   12

可以使用 reshape 组合前三行。由于 Matlab 按列工作,因此您实际上需要将 reshape 应用于 A 的转置:

octave-3.0.0:24> B = reshape(A', 6, 2)'
B =

    1    7    2    8    3    9
    4   10    5   11    6   12

要重新定位列,只需使用 Matlab 良好的索引功能即可。给出一个具有所需顺序的向量作为列索引,通过给出冒号 : 作为行索引来获取所有行:

octave-3.0.0:25> B(:,[5,6,1:4])
ans =

    3    9    1    7    2    8
    6   12    4   10    5   11

You can manipulate the arrays fairly easily using Matlab, no loops needed. I'll use a smaller array to make it easier to see what's going on. I'll also use Octave, but it makes no difference here. I'll also assume the matrix is already available in a variable (use load to do this, it's much easier than approach in the question).

First, consider a matrix A:

octave-3.0.0:23> A
A =

    1    7
    2    8
    3    9
    4   10
    5   11
    6   12

Combining the first three rows can be done with reshape. Since Matlab works column-wise, you'll actually need to apply reshape to the transpose of A:

octave-3.0.0:24> B = reshape(A', 6, 2)'
B =

    1    7    2    8    3    9
    4   10    5   11    6   12

To reposition columns, just use Matlab's nice indexing abilities. Give a vector with the desired order as the column index, taking all rows by giving a colon : as the row index:

octave-3.0.0:25> B(:,[5,6,1:4])
ans =

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