我有一个包含所有数字数据的文本文件,每行代码如下所示
标签index1:value1 index2:value2。 。 。 index8:value8
示例行:
1 1:0.731641 2:0.0445045 3:0.959925 4:1 6:0.366283 7:0.304412 8:0.616054
任何索引上的行都可能缺少值,如何使用此文件形成9列的矩阵?
答案 0 :(得分:1)
您可以使用textscan
和%d:%f
格式说明符来获取此信息。这是一个完整的例子,说明如何做到这一点。
fid = fopen(filename, 'r');
%// Get the label of the first line
label = fscanf(fid, '%d', 1);
%// Initialize matrix to hold the result
%// If you know what the maximum label will be you can make this the right size
m = zeros(0,9);
%// Row counter
row = 1;
%// Loop until we hit the end of the file
while ~feof(fid)
%// Look for a number followed by a colon and another number
data = textscan(fid, '%d:%f');
values = data{2};
columns = data{1}(1:numel(values));
%// Fill the matrix at the columns that we loaded using the label as the row
m(row, columns) = values;
%// The next label was automatically picked up because it matched %d
row = row + 1;
end
fclose(fid);
我在以下输入上对此进行了测试:
1 1:0.73 2:0.04 3:0.95 4:1 6:0.36 7:0.30 8:0.61
2 2:0.99 2:0.45 5:0.99 4:2 7:0.5 9:10
3 1:-2 9:12
并且能够生成以下矩阵
m =
0.73 0.04 0.95 1 0 0.36 0.3 0.61 0
0 0.45 0 2 0.99 0 0.5 0 10
-2 0 0 0 0 0 0 0 12
<强>更新强>
如果你有一个非常大的矩阵并且它是稀疏填充的,你可以使用类似下面的东西从数据中创建一个稀疏矩阵。
fid = fopen(filename, 'r');
%// Get the label of the first line
label = fscanf(fid, '%d', 1);
%// Initialize rows/cols/values array
[rows, cols, vals] = deal([]);
%// Row counter
row = 1;
%// Loop until we hit the end of the file
while ~feof(fid)
%// Look for a number followed by a colon and another number
data = textscan(fid, '%d:%f');
columns = data{1}(1:numel(data{2}));
%// Append data to rows/cols/vals arrays
vals = cat(1, vals(:), data{2});
cols = cat(1, cols(:), double(columns(:)));
rows = cat(1, rows(:), row + zeros(size(columns(:))));
%// The next label was automatically picked up because it matched %d
row = row + 1;
end
fclose(fid);
%// Create the sparse matrix
m = sparse(rows, cols, vals);