开发者

How to re-create a matrix from indices and values froma text file in matlab

开发者 https://www.devze.com 2023-03-07 08:00 出处:网络
I wish to do the following in matlab: -I have a text file with the following format: 110.242 120.332 ...

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 re开发者_StackOverflow中文版ad this data in and re-create the matrix in matlab so i can do operations on it. Any suggestions?


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)


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.


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.


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 ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消