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

4 ビュー (過去 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 Center および File ExchangePolynomials についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by