Why does MATLAB compute an equation involving a scalar and a matrix by algebraically working on the individual elements?

2 次查看(过去 30 天)
I was working on curve fitting using Hermite polynomial where i had to compute a matrix equation (1-[vector]), where 'vector' represents a polynomial. The idea is to subtract the 1 with the 0 order term in the vector and get a new list of coefficients.The line of code was wrong and i changed the code to get the right result but i wonder why MATLAB didn't throw an error. E.g 1-[1 2]= 0 -1 according to MATLAB. Why does it go through?

采纳的回答

Walter Roberson
Walter Roberson 2016-11-27
It has been defined that way for a long long time. See for example the first example https://www.mathworks.com/help/matlab/ref/minus.html#examples
The operations + - .* .^ and ./ all automatically do expansion of scalars on one side to match the size of the other. The operation * with a scalar on one side is automatically treated as .* (that is, each element of the other side is multiplied by the scalar.) The operation / with a vector on the left side and a scalar on the right side is automatically treated as ./ (that is, each element of the left side is divided by the scalar.)
The one common situation that is not automatically expanded is / with scalar on the left and row vector on the right (which is an error.)
  1 个评论
Abhishek Pandey
Abhishek Pandey 2016-11-27
Yes, i actually went to the documentation mentioned by you after i found the problem in my code and checked it. Probably, it was wrong on my part to expect that the operation would follow all the matrix properties. Thank you very much.

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by