1) We have defined an array of length 5, but have asked MATLAB to display the value at index 6, which doesn't exist. We will get an error letting us know that index 6 doesn't exist.
an_array = rand(1,5);
disp(an_array(6))
error: an_array(6): out of bound 5
2) We are trying to multiply matrices that don't have conforming dimensions - A
is a 3-by-3 matrix and b
is a length-4 column vector, so we can't perform A*b
. We will get a "nonconforming objects" error.
A = rand(3,3);
b = rand(4,1);
x = A*b;
error: operator *: nonconformant arguments (op1 is 3x3, op2 is 4x1)
3) Again, we are trying to element-wise subtract two arrays, but they are not the same shape so we can't element-wise subtract because we don't have enough elements in one of the arrays. We will get a "nonconforming objects" error.
v1 = rand(1,5);
v2 = rand(1,10);
disp(v1-v2)
error: operator -: nonconformant arguments (op1 is 1x5, op2 is 1x10)
4) Trick question... this one is actually fine, although MATLAB will give you a warning instead of an error, which we will talk about later. Notice that although A
is 3-by-3 and b
is 5-by-1, the slicing b(1:3)
happens before the backslash \
operation, so we are solving Ax = b(1:3)
, which is perfectly fine because b(1:3)
is 3-by-1! Of course, the warning we get should concern us though.
A = rand(6,6);
A(2:4,2:4) = [1 2 3; 4 5 6; 7 8 9];
b = ones(7,1);
x = A(2:4,2:4)\b(1:3);
warning: matrix singular to machine precision