assign the result of inverse matrix to vector

1 次查看(过去 30 天)
A= [4 1 0 0; 1 3 0.5 0; 0 0.5 2 0.5; 0 0 0.5 3];
b= [7.2 -6.6 3.6 3]';
[m1,m2,m3,m4]' = inv(A)*b ; <- here is the error
I tried to put
[m1,m2,m3,m4] = inv(A)*b ; and
[m1;m2;m3;m4] = inv(A)*b ;
but none of them work.
the result of inv (A) * b is:
ans =
     2.6788
    -3.5154
     2.5344
     0.5776
  1 个评论
Stephen23
Stephen23 2017-9-27
编辑:Stephen23 2017-9-27
Firstly, this is a bad way to solve equations:
inv(A)*b
You should read the inv documentation to know why, and what the better alternatives are (hint: mldivide).
Secondly, this is likely a bad way to use MATLAB:
[m1,m2,m3,m4]' = ...
Why bother? The name MATLAB comes from "MATrix LABoratory", not from "Let split the data up into lots of separate variables and make the code slow and complex". MATLAB works very efficiently with matrices, why do you want to split your data into hard-to-use separate variables?
Learn to use matrices/arrays and indexing, and then you will learn how to write neat, simple, efficient MATLAB code. To make code simpler and more efficient always keep data together as much as possible, rather than splitting it apart.
Putting data into numbered variables is also a very bad idea, as it inevitably leads beginners to writing slow, buggy code using bad code practices. Read this to know more:

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2017-9-27
You should use, a single variable name to store it....
B = inv(A)*b ;
You can take the output like that....if you are still insisting on m1,m2,m3,m4....
m1 = B(1) ;
m2 = B(2) ;
m3 = B(3) ;
m4 = B(4) ;
Note that there is no requirement of storing each value of an array into separate variables.
  1 个评论
Jan
Jan 2017-9-27
+1. Prefer m = A \ b and use an index later: m(2) instead of m2. This is more flexible and efficient.
By the way: inv(A)*b is less efficient and stable than A\b. See: doc inv

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Cash Flows 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by