如何在MATLAB中加载这种矩阵 [英] How to load this kind of matrix in MATLAB

查看:197
本文介绍了如何在MATLAB中加载这种矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上三角矩阵,如:

  A =负载( 'A.TXT'); 

1.0 3.32 -7.23
1.00 0.60

我想加载它在MATLAB中,因为这是对称转换它做
$ b $ p $ c $ a = A + triu(A,1)'

所以最后我会有一个矩阵 a

  1.0 3.32 -7.23 
3.32 1.00 0.60
-7.23 0.60 1.00

我现在遇到的问题是在加载时:

 >> A = load('A.txt'); 
?错误使用==>加载
ASCII文件第一行的列数C:\ A.txt
必须与前面的行相同。

有没有办法达到这个效果?

解决方案

请尝试使用 importdata 相反,加载通常只用于 .mat em>文件。你的文件 A.txt 结构如何?如果是这样,

  1.0 3.32 -7.23 
1.00 0.60
1.00

然后您将获得

  A = importdata('A.txt')

A =

1.0000 3.3200 -7.2300
1.0000 0.6000
1.0000 NaN NaN

所以你必须移动最后两行,像这样


$ b $($,$ $ $ $ $ $ $ $ $ $ $ $ $ $ $) 3,:),[0 2])

A =

1.0000 3.3200 -7.2300
NaN 1.0000 0.6000
NaN NaN 1.0000

然后用0替换NaNs并使用您的表达式来创建对称矩阵。

  A(isnan(A))= 0; 

a = A + triu(A,1)';

A =

1.0000 3.3200 -7.2300
3.3200 1.0000 0.6000 $ b $ -7.2300 0.6000 1.0000
pre>

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 to load it in MATLAB, and as this is symmetric converting it doing

a = A + triu(A, 1)'
so at the end I will have a matrix a

1.0    3.32   -7.23
3.32   1.00    0.60
-7.23  0.60    1.00

the problem I am having is at the moment of loading:

>> A = load('A.txt');
??? Error using ==> load
Number of columns on line 1 of ASCII file C:\A.txt
must be the same as previous lines.

Is there a way to accomplish this?

解决方案

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

这篇关于如何在MATLAB中加载这种矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆