Write lines of code that generate each of the following
array-related errors:
??? Error: Unbalanced or unexpected parenthesis or bracket.
??? Attempted to access A(-1); index must be a positive
integer
or logical.
??? Error using ==> horzcat CAT arguments dimensions are
not
consistent.
??? Error using ==> vertcat CAT arguments dimensions are
not
consistent.
??? In an assignment A(I) = B, the number of elements in B
and
I must be the same.
??? Error using ==> plus Matrix dimensions must agree.
??? Attempted to access A(4); index out of bounds because
numel(A)=3.
1). ??? Error: Unbalanced or unexpected parenthesis or bracket
When the number of left paranthesis and right parenthesis does not match then this error occurs.
A =[1 2 3 4];
Array indices must be positive integers or logical values.
A(1:
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
2). ??? Attempted to access A(-1); index must be a
positive integer
or logical.
There are no negative indexing. So we will get error when we try to access using negative indexes
A =[1 2 3 4 5 6];
A(-1)
Array indices must be positive integers or logical values.
3). ??? Error using
==> horzcat CAT arguments dimensions are not
consistent.
When number of rows in A and B are not equal and we use horzcat() then we will get this error.
A = [1 2 3 ; 4 5 6];
B = [10 11 12; 12 13 14;14 15 16];
horzcat(A,B)
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
4). ??? Error using ==> vertcat CAT arguments
dimensions are not
consistent.
When number of columns in A and B are not equal and if we use vertcat() then we will get this error.
A = [1 2 3 ; 4 5 6];
B = [10 11 ; 12 13 ;14 15 ];
vertcat(A,B)
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
5). ??? In an assignment A(I) = B, the number of
elements in B and
I must be the same.
When we assign more than one elements to a single position then we will get this error.
In the following example we are assigning a vector B to the first position of A. SO it gives error.
A = [1 2 3 4 5 6];
B = [7 8 9 10];
A(1) = B
Unable to perform assignment because the left and right sides have a different number of elements.
6). ??? Attempted to access A(4); index out of bounds
because
numel(A)=3.
When we are trying to access an index which is not present we will get this error.
A = [1 2 3];
A(4)
Index exceeds the number of array elements (3).
Get Answers For Free
Most questions answered within 1 hours.