Matrix subtraction within Matlab
이전 댓글 표시
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
madhan ravi
2019년 8월 29일
Illustrate.
KALYAN ACHARJYA
2019년 8월 29일
편집: KALYAN ACHARJYA
2019년 8월 29일
Is this way, are you trying?
xT=randi(10,3,1) % Random integer 3x1 mat
x=randi(20,3,1) % % Random integer 3x1 another mat
Er=xT-x
Command Window:
xT =
7
8
3
x =
1
3
16
Er =
6
5
-13
Monish Sengupta
2019년 8월 29일
James Tursa
2019년 8월 29일
편집: James Tursa
2019년 8월 29일
It isn't possible, of course. You are either not doing the coding the way you think you are, or you are misinterpreting the displayed results. Instead of "describing" to us what you think you are doing, just copy & paste the actual code and actual output. If you need to attach a mat file for the values, do that as well. You don't provide enough digits in your post for us to guess what is going on.
Monish Sengupta
2019년 8월 29일
편집: Stephen23
2019년 8월 29일
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
2019년 8월 29일
답변 (1개)
John D'Errico
2019년 8월 29일
편집: John D'Errico
2019년 8월 29일
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.
카테고리
도움말 센터 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!