How to read a triangular matrix

조회 수: 10 (최근 30일)
Peter Kerekes
Peter Kerekes 2018년 4월 20일
편집: Guillaume 2018년 4월 20일
Hello ! I would like to read an upper triangular(symmetrical) matrix from a txt the format is the forwarding:
A=[1 1 1;
' ' 1 1;
' ' ' ' 1;];
The upper triangle element are missing(' ' is a space character instead) in the txt.
  댓글 수: 2
Guillaume
Guillaume 2018년 4월 20일
To avoid ambiguity it would be better if you attached an example text file.
Peter Kerekes
Peter Kerekes 2018년 4월 20일
I only have a very big txt, but I can atteche it. It is an 1000x1000 matrix

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2018년 4월 20일
Read the values as a single vector. Delete the entries corresponding to the 0 diagonal (I did not look through the entire file to be sure the diagonal is all 0 though.) Noe you should be able to use squareform() to create the full array.
squareform() is mostly used with pdist()

Guillaume
Guillaume 2018년 4월 20일
편집: Guillaume 2018년 4월 20일
fid = fopen('Stext2.txt');
entries = textscan(fid, '%f');   %read every number as one vector
fclose(fid);
msize = (sqrt(8*numel(entries{1})+1)-1)/2;  %size of full matrix
fullmat = zeros(msize);         
fullmat(tril(true(size(fullmat)))) = entries{1};  %fill up lower triangle (since matlab fills columns not rows)
fullmat = fullmat + triu(fullmat.', 1);  %add upper triangle (without main diagonal)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by