The error refers to the snippet (x-k11)^2
, where you subtract a scalar (k11
) from an array (x
, 1x36) and try to square the result. The problem is that the function ^
is the shortcut for the function mpower()
, which is the matrix-power function and consequently expects a scalar or a matrix calculation as it essentially is
x^2 == x*x
x^3 == x*x*x
However, it does not know what to do with an array as x*x
does not work (try do run rand(1,36)*rand(1,36)
, which will essentially raise the same error).
It also suggests a solution: .^
, which is the element-wise power function (in fact, the .
in an arithmetic operation usually indicates that the following operation is conducted element-wise). This .^
is the shortcut for the "normal" power
function as you would have expected in the first place. It performs the ^2
to every element of the array x
.
x.^2 == power(x,2)
extended side note:
To mimic the behavior of the element-wise operator .
, you may want to have a look at the arrayfun
function, which applies a certain function to every element of a matrix or array/vector. If you are a new with matlab (as I assume from your question), this hint may just confuse you.
x.^2 == arrayfun(@(a)mpower(a,2),x)