Matrix subtraction within Matlab

8 visualizaciones (últimos 30 días)
Monish Sengupta
Monish Sengupta el 29 de Ag. de 2019
Comentada: Monish Sengupta el 29 de Ag. de 2019
I am generating a 3X1 matrix and name it 'xT'. Simultaneously I am generating another 3X1 matrix and name it 'x'. When I am computing the difference between these two i.e. Er = xT - x within Matlab, the answer is different than individual subtraction. Therefore when I am subtracting xT(2,1) - x(2,1), I am getting a different answer when Matlab is computing the Er. Please help.
  7 comentarios
Stephen23
Stephen23 el 29 de Ag. de 2019
Editada: Stephen23 el 29 de Ag. de 2019
@Monish Sengupta : please also upload your data in a .mat file.
Not in a text file, an excel file, or anything else. In a .mat file.
We cannot check your results without your data.
Monish Sengupta
Monish Sengupta el 29 de Ag. de 2019
I have no uploaded the .mat file.

Iniciar sesión para comentar.

Respuestas (1)

John D'Errico
John D'Errico el 29 de Ag. de 2019
Editada: John D'Errico el 29 de Ag. de 2019
whos
Name Size Bytes Class Attributes
E 2x1 16 double
E11 1x1 8 double
E21 1x1 8 double
Er 3x1 24 double
Er11 1x435 3480 double
Er21 1x435 3480 double
Er31 1x435 3480 double
ErA 3x1 24 double
ErA11 1x434 3472 double
ErA21 1x434 3472 double
ErA31 1x434 3472 double
ErB 3x1 24 double
ErB11 1x434 3472 double
ErB21 1x434 3472 double
ErB31 1x434 3472 double
ErC 3x1 24 double
ErC11 1x434 3472 double
ErC21 1x434 3472 double
ErC31 1x434 3472 double
H 2x3 48 double
I 3x3 72 double
InA 2x1 16 double
InA11 1x434 3472 double
InA21 1x434 3472 double
...
This, by the way, is why you DON'T ever want to create such reams of numbered variables. That is incredibly bad programming style, as it will cuse you to make mistake after mistake, as you type the wrong variable name. LEARN TO USE VECTORS. Better, learn to use arrays.
Regardless, you only think it did something different, probably because of the many different variable names, and possibly because you are using a short display format.
format long g
>> x
x =
10869.0885676116
0.228888860503846
-1.08890062860864
xt
xt =
10869.05
-0.299999999999384
-1.1
xt - x
ans =
-0.0385676116566174
-0.528888860503229
-0.0110993713913601
xt(2) - x(2)
ans =
-0.528888860503229
The results will be identical. The only problem is that sometimes when you display a vector as a result, it might get displayed so it looks differently, especially if you are only showing 4 digits.
A frequent and similar mistake made by new users at MATLAB, is they will display the numbers using format short, as:
format short
>> x
x =
1.0e+04 *
1.0869
0.0000
-0.0001
>> xt
xt =
1.0e+04 *
1.0869
-0.0000
-0.0001
and then either miss the 1.0e+4 at the top, or they will think those numbers are exactly what they see, then do a subtraction by hand.
Sorry, but these are NOT MATLAB mistakes. It is a user error, with 100% probability. I won't even call it probable user error, as that would imply some vague chance that it was not a user error. As you develop better skills at MATLAb, you will learn to make better use of your variables, but you will also learn to go more slowly, to check your work more carefully.
  1 comentario
Monish Sengupta
Monish Sengupta el 29 de Ag. de 2019
Thank you for your helpful comments. I have managed to find the problem.

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations 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