如何在 matlab 中从文本文件中的索引和值重新创建矩阵

发布于 2024-11-09 07:25:43 字数 410 浏览 0 评论 0原文

我希望在matlab中执行以下操作: -我有一个格式如下的文本文件:1 1 0.242 1 2 0.332 ... 75 74 0.485 75 75 0.726 - 第一列是矩阵的第 i 个索引的列表,第二列是矩阵的第 j 个索引,第三列是特定索引 (i,j) 处的值。简而言之,我有一个 75x75 矩阵,其值位于文本文件中。 -我希望读入这些数据并在 matlab 中重新创建矩阵,以便我可以对其进行操作。 有什么建议吗?

I wish to do the following in matlab:
-I have a text file with the following format: 1 1 0.242
1 2 0.332
...
75 74 0.485
75 75 0.726
-The first column is a list of ith index of a matrix, the second coloumn is the jth index of a matrix, and the third column is the value at the particular index (i,j). In short, I have a 75x75 matrix with its values in a text file.
-I wish to read this data in and re-create the matrix in matlab so i can do operations on it.
Any suggestions?

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

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

发布评论

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

评论(4

撧情箌佬 2024-11-16 07:25:43

您所描述的是一个存储稀疏矩阵(以元组格式)的文件。稀疏矩阵是大多数元素为 0 的矩阵,因此为了节省空间,您只存储非零元素。 Matlab 有一个内置的稀疏矩阵对象。您可以直接加载文件:

>> load matrix.mtl;
>> A = spconvert(matrix);

请参阅:http://bebop.cs.berkeley。 edu/smc/formats/matlab.html

您可能需要注意的另一件事是稀疏函数,它接受三个向量并将它们转换为稀疏矩阵。

稀疏矩阵可以使用 full(A) 转换为密集矩阵(即二维数组,这就是大多数人所说的矩阵)

What you describe is a file storing a sparse matrix (in tuples format). A sparse matrix is a matrix where most elements are 0, so to save space you only store the nonzero elements. Matlab has a built-in sparse matrix object. You can load your file directly:

>> load matrix.mtl;
>> A = spconvert(matrix);

see: http://bebop.cs.berkeley.edu/smc/formats/matlab.html

Another thing you might want to be aware of is the sparse function which takes three vectors and turns them into a sparse matrix.

A sparse matrix can be converted into a dense matrix (i.e. a 2D array, which is what most people mean when they say matrix) use full(A)

全部不再 2024-11-16 07:25:43

要在 MATLAB 中重新创建完整的 75×75 矩阵,您可以首先使用函数 LOAD

data = load('datafile.txt','-ascii');

然后通过将下标索引转换为 线性索引 使用函数 SUB2IND 或您自己的简单计算:

mat = zeros(75);  %# Initialize your matrix to zeroes
mat(sub2ind([75 75],data(:,1),data(:,2))) = data(:,3);  %# Use SUB2IND...
%# ... or...
mat(75.*(data(:,2)-1)+data(:,1)) = data(:,3);  %# ... convert them yourself.

To recreate a full 75-by-75 matrix in MATLAB, you can first load your file using the function LOAD:

data = load('datafile.txt','-ascii');

Then recreate your matrix (without the need of a for loop) by converting your subscript indices into linear indices using either the function SUB2IND or a simple computation of your own:

mat = zeros(75);  %# Initialize your matrix to zeroes
mat(sub2ind([75 75],data(:,1),data(:,2))) = data(:,3);  %# Use SUB2IND...
%# ... or...
mat(75.*(data(:,2)-1)+data(:,1)) = data(:,3);  %# ... convert them yourself.
只涨不跌 2024-11-16 07:25:43

这取决于您想要以交互方式还是以编程方式执行此操作。

交互式:

使用向导导入文本文件。您将得到一个 nx3 值矩阵(假设data)。然后使用一个简单的 for 循环将所有值放在正确的位置。

A = zeros(75, 75);
for idx = 1:size(data, 1)
    A(data(idx, 1), data(idx, 2)) = data(idx, 3);
end

以编程方式:

使用 textscan 例如。然后按照上面的方法进行。

It depends on whether you want to do it interactively or programmatically.

Interactively:

Import the text file using the wizard. You'll get a nx3 matrix (let's say data) of values. Then use a simple for loop the put all the values at their right place.

A = zeros(75, 75);
for idx = 1:size(data, 1)
    A(data(idx, 1), data(idx, 2)) = data(idx, 3);
end

Programmatically:

Do the import of the file programmatically using textscan for example. Then proceed as above.

陈年往事 2024-11-16 07:25:43

假设您的文件名为“data.txt”并且它仅包含您提到的格式化信息,我认为最好的方法是使用如下示例代码:

fid=fopen('data.txt','r');
data=fscanf(fid,'%g',[3 Inf]);
fclose(fid);
[m n]=size(data);
a=zeros(75,75);
for i=1:n
    a(data(1,i),data(2,i))=data(3,i);
end

如果文件还包含其他信息,则应注意收集适当的线路和数据。
作为您提供的少数样本的结果示例,数据矩阵将

data =
1.0000    1.0000   75.0000   75.0000
1.0000    2.0000   74.0000   75.0000
0.2420    0.3320    0.4850    0.7260

让我们知道是否存在其他问题。祝你好运 ;)

Assuming your file is named "data.txt" and it contains only the formatted information as you mentioned, I think the best way to do this, is a sample code like this:

fid=fopen('data.txt','r');
data=fscanf(fid,'%g',[3 Inf]);
fclose(fid);
[m n]=size(data);
a=zeros(75,75);
for i=1:n
    a(data(1,i),data(2,i))=data(3,i);
end

If the file contains other information also, care should be taken to collect the appropriate lines and data.
As an example of results with the few samples you provided, the data matrix will be

data =
1.0000    1.0000   75.0000   75.0000
1.0000    2.0000   74.0000   75.0000
0.2420    0.3320    0.4850    0.7260

Let us know if there are other problems. Good Luck ;)

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