![]() |
VOOZH | about |
A Matrix is a two-dimensional array of elements. In MATLAB (short for Matrix Laboratory), the matrix is created by assigning the array elements that are delimited by spaces or commas and using semicolons to mark the end of each row.
A matrix can be created by listing elements inside square brackets [ ].
A = [elements; elements]
Example: Creating a Matrix
Output:
x =
1 2 3
4 5 6
7 8 9
y =
Geeks
Geeks
You can find the dimensions (rows × columns) of a matrix using the size() function.
Example:
Output:
xSize =
3 4
ySize =
2 5
To access specific elements, use the syntax:
matrix(row, column)
Example 1: Access a Single Element
Output:
ans =
8
Example 2: Accessing Multiple Elements
To access multiple elements:
Output:
ans =
1 2 3 4
4 5 6 7
7 8 9 10
ans =
1 2 3 4
4 5 6 7
ans =
2 3 4
5 6 7
8 9 10
ans =
2 3
5 6
You can add rows or columns to a matrix using concatenation ([]) or the cat() function.
cat(dimension, A, B, ...)
Example 1: Using Brackets
Output:
a =
1 2 3 4
4 5 6 7
7 8 9 10
11 12 13 14
b =
1 2 3 4
4 5 6 7
11 12 13 14
x =
1 2 3 4 15
4 5 6 7 16
Example 2: Using cat()
Output:
a =
1 2 3 4
4 5 6 7
1 2 3 4
4 5 6 7
b =
1 2 3 4 1 2 3 4
4 5 6 7 4 5 6 7
You can remove rows or columns by assigning them an empty array [].
Example: