why is the answer 0.8058 ?
Afficher commentaires plus anciens
Hi, i am new to matlab and i am trying to understand this code below. I know it is a matrix but i still don't understand why the answer is 0.8058. How does it work?
DD = 1:1:6
D= 1:1:6
X(DD)=D(DD)/ (D+1)
------------------------------------------------------
x = 0.8058 0.8058 0.8058 0.8058 0.8058 0.8058
1 commentaire
William Rose
le 3 Avr 2021
@Ronald Eboue Teto, This was a good question. The answer was not obvious to me. See my answer below for why x=.8058. I learned something by answering this.
Réponses (3)
Image Analyst
le 3 Avr 2021
You're doing a matrix division not an element by element division. Is that what you expected? What did you expect? Did you want this instead?
DD = 1:1:6
D= 1:1:6
X(DD) = D(DD) ./ (D+1)
X =
0.5 0.66667 0.75 0.8 0.83333 0.85714
William Rose
le 3 Avr 2021
Modifié(e) : William Rose
le 3 Avr 2021
2 votes
You get the same answer by doing
mrdivide(D,D+1)
See the man page for matrix right divide.
Matlab finds the value of x that gives the least-squares solution to
x(D+1)=D, where D and D+1 are vectors. You can show by calculus that the least squares solution to the above
is
x=dot(D,D+1)/dot(D+1,D+1).
And if you try it in Matlab, you get the same answer, x=0.8058.
1 commentaire
William Rose
le 3 Avr 2021
x=B/A finds the least squares solution to xA=B.

where A, B are column vectors and x is a scalar. It also works for row vectors, just flip which (xA-B) is transposed.


and this derivative=0 when SumSqErr is minimized. Therefore the minimum is at
, i.e. x=dot(A,B)/dot(A,A)
In this case, we have
x=D/(D+1);
which means A corresponds to D+1 and B corresponds to D, i.e.
x=dot(D+1,D)/dot(D+1,D+1)
It's a vector because you specified that it must be. Let's start off by simplifying things:
D(DD) is D(1:6). Since D has six elements, D(DD) is the same as D.
Similarly, X is defined as a vector having six elements, and the value of each element is the result of the RHS of the expression. It's the same as doing
X=zeros([1 6]);
X(:)=D/(D+1)
If you expect X to be a scalar, just use the result directly
D= 1:6
X=D/(D+1)
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!