How to load this kind of matrix in MATLAB

前端 未结 1 1240
北恋
北恋 2021-01-23 12:34

I have an upper triangular matrix like:

A=  load(\'A.txt\');

1.0    3.32   -7.23
       1.00    0.60
               1.00

I want t

相关标签:
1条回答
  • 2021-01-23 13:05

    Try using importdata instead, load is usually only used for .mat files. How is your file A.txt structured? If it is like this,

    1.0    3.32   -7.23
    1.00    0.60
    1.00
    

    then you will get

    A = importdata('A.txt')
    
    A =
    
        1.0000    3.3200   -7.2300
        1.0000    0.6000       NaN
        1.0000       NaN       NaN
    

    So you will have to shift the two last rows, like this

    A(2,:) = circshift(A(2,:),[0 1])
    A(3,:) = circshift(A(3,:),[0 2])
    
    A =
    
        1.0000    3.3200   -7.2300
        NaN       1.0000    0.6000
        NaN       NaN       1.0000
    

    and then replace the NaNs with 0s and use your expression to create a symmetric matrix.

    A(isnan(A)) = 0;
    
    a = A + triu(A, 1)';
    
    A =
    
        1.0000    3.3200   -7.2300
        3.3200    1.0000    0.6000
       -7.2300    0.6000    1.0000
    
    0 讨论(0)
提交回复
热议问题