如何在 matlab 中从文本文件中的索引和值重新创建矩阵
我希望在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您所描述的是一个存储稀疏矩阵(以元组格式)的文件。稀疏矩阵是大多数元素为 0 的矩阵,因此为了节省空间,您只存储非零元素。 Matlab 有一个内置的稀疏矩阵对象。您可以直接加载文件:
请参阅: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:
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)
要在 MATLAB 中重新创建完整的 75×75 矩阵,您可以首先使用函数 LOAD:
然后通过将下标索引转换为 线性索引 使用函数 SUB2IND 或您自己的简单计算:
To recreate a full 75-by-75 matrix in MATLAB, you can first load your file using the function LOAD:
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:
这取决于您想要以交互方式还是以编程方式执行此操作。
交互式:
使用向导导入文本文件。您将得到一个 nx3 值矩阵(假设
data
)。然后使用一个简单的 for 循环将所有值放在正确的位置。以编程方式:
使用
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.Programmatically:
Do the import of the file programmatically using
textscan
for example. Then proceed as above.假设您的文件名为“data.txt”并且它仅包含您提到的格式化信息,我认为最好的方法是使用如下示例代码:
如果文件还包含其他信息,则应注意收集适当的线路和数据。
作为您提供的少数样本的结果示例,数据矩阵将
让我们知道是否存在其他问题。祝你好运 ;)
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:
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
Let us know if there are other problems. Good Luck ;)