I have text file (66*3 matrix), the first column is the row # , the second column is column # and the 3rd column is a value, how can I populate another matrix with the info given in the text file using for loop or any other method.
Hope I conveyed my message well.
Thanks

1 件のコメント

Kevin
Kevin 2013 年 3 月 2 日
編集済み: Walter Roberson 2013 年 3 月 2 日
Im gonna go with a smaller sample to clarify like 5*3 text file,
test.txt = [ 0 0 78
;1 0 24
;1 1 25
;2 0 18
;2 1 -54 ]
so the new populated matrix would be NewMatrix = [78 0 0 ;24 25 0 ;18 -54 0]
Thanks

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2013 年 3 月 2 日

1 投票

rcv = reshape(sscanf(fileread('YourTextFile.txt'),'%f'),3,[]).';
YourMatrix = accumarray(rcv(:,1:2), rcv(:,3));

5 件のコメント

Kevin
Kevin 2013 年 3 月 2 日
thanks for your answer but it did not work
Walter Roberson
Walter Roberson 2013 年 3 月 2 日
0 is not a row or column number.
Does your text file contain the '[' and ']' and the ';' ? Or does it contain only what was described, as in
1 1 78
2 1 24
2 2 25
3 1 18
3 2 -54
?
Notice the correction of the erroneous row and column inputs you had.
Kevin
Kevin 2013 年 3 月 2 日
Hey, no its just separated by space, only what it was described, but there are zeroes in there though, what should I do?
Kevin
Kevin 2013 年 3 月 2 日
編集済み: Kevin 2013 年 3 月 2 日
Okay I fixed it now it is exactly like
1 1 78 2 1 24 2 2 25 3 1 18 3 2 -54
now How do I turn it to
78 0 0 24 25 0 18 -54 0 ? thanks
Kevin
Kevin 2013 年 3 月 2 日
Thanks after a lot of trial and errors it worked thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で MATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by