Creating an array of numbers in MATLAB is straightforward using square brackets []
.
You can create arrays (which are a fundamental type of matrix in MATLAB) by enclosing the numbers within square brackets. The elements are separated by spaces or commas for row elements, and semicolons ;
to indicate the end of a row.
Basic Methods for Creating Arrays
Based on standard MATLAB syntax, including the examples provided:
1. Creating a Row Array
To create an array with elements arranged in a single row, you list the elements inside square brackets []
, separating them with spaces or commas.
- Syntax:
variableName = [element1 element2 element3 ...]
- Example (from reference):
a = [1 2 3 4]
This creates a row array named
a
:a = 1 2 3 4
2. Creating a Column Array
To create an array with elements arranged in a single column, you list the elements inside square brackets []
, separating each element with a semicolon ;
.
- Syntax:
variableName = [element1; element2; element3; ...]
- Example (from reference):
a = [1; 2; 3; 4]
This creates a column array named
a
:a = 1 2 3 4
3. Creating a 2D Matrix (Multi-row Array)
To create a matrix with multiple rows and columns, you list the elements row by row. Elements within a row are separated by spaces or commas, and rows are separated by semicolons ;
. All rows must have the same number of elements.
- Syntax:
variableName = [row1_elements; row2_elements; ...]
- Example (from reference):
a = [1 2 3; 4 5 6; 7 8 9]
This creates a 3x3 matrix named
a
:a = 1 2 3 4 5 6 7 8 9
Summary of Array Creation Syntax
Here's a quick reference for the basic syntax mentioned:
Type of Array | Separators Used | Example based on Reference | Result Shape |
---|---|---|---|
Row Array | Space or Comma (, ) |
a = [1 2 3 4] |
1 x N |
Column Array | Semicolon (; ) |
a = [1; 2; 3; 4] |
N x 1 |
2D Matrix | Space/Comma and Semicolon | a = [1 2 3; 4 5 6] |
M x N (M>1, N>1) |
Note: The reference specifically shows [1 2 3; 4 5 6; 7 8 9]
for a 3x3 matrix.
These fundamental methods using square brackets are the most common ways to directly define small arrays and matrices with specific numerical values in MATLAB.
For more details and other methods (like using functions for sequences or special matrices), you can consult the MATLAB documentation on creating matrices and arrays.