assign the result of inverse matrix to vector

5 visualizaciones (últimos 30 días)
Emilio Ortega
Emilio Ortega el 27 de Sept. de 2017
Editada: Emilio Ortega el 27 de Sept. de 2017
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 comentario
Stephen23
Stephen23 el 27 de Sept. de 2017
Editada: Stephen23 el 27 de Sept. de 2017
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:

Iniciar sesión para comentar.

Respuestas (1)

KSSV
KSSV el 27 de Sept. de 2017
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 comentario
Jan
Jan el 27 de Sept. de 2017
+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

Iniciar sesión para comentar.

Categorías

Más información sobre Assembly en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by