In Matlab, I have typed the following commands:
>> a = [1 2; 3 4]
a =
1 2
3 4
When I tried the command a^2, I got the following:
>> a^2
ans =
7 10
15 22
I was actually expecting to get:
ans =
1 4
9 16
In other words, I was expecting to get each element of the matrix to be raised to 2.
Why was the result as shown above?
Thanks.
In MATLAB, all single-character operators are matrix operators. So, you are using the matrix power, e.g.,
if you want to square each element, you’ll have to use the element-wise power operator:
Read more about MATLAB’s operators here.