如何按以下方式操作数据?
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在使用您提供的示例数据文件作为示例。 TEXTSCAN 函数用于解析文件
data.dat
MATLAB
代码由于数据包含异构类型(数字和字符),因此我们单独读取和存储它们。最后一行代码显示了将所有数据合并到单个元胞数组中的一种方法:
I am using the sample data file you've given as an example. The TEXTSCAN function is used to parse the file
data.dat
MATLAB code
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:
您可以使用 Matlab 相当轻松地操作数组,无需循环。我将使用较小的数组来更容易地看到发生了什么。我也会使用 Octave,但在这里没有什么区别。我还将假设矩阵已经在变量中可用(使用
load
来执行此操作,它比问题中的方法容易得多)。首先,考虑一个矩阵
A
:可以使用
reshape
组合前三行。由于 Matlab 按列工作,因此您实际上需要将reshape
应用于A
的转置:要重新定位列,只需使用 Matlab 良好的索引功能即可。给出一个具有所需顺序的向量作为列索引,通过给出冒号
:
作为行索引来获取所有行: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
:Combining the first three rows can be done with
reshape
. Since Matlab works column-wise, you'll actually need to applyreshape
to the transpose ofA
: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: