Matlab is a technical computing program and programming language for engineers. The technical computing part of the software is matrix-based. This means that variables are saved and manipulated as matrices. Matlab allows users to use matrices of a variety of different dimensions and data types. One-dimensional matrices (i.e., arrays) and two-dimensional matrices with numerical data are most often used, but higher dimensions can be used as well. The following shows you how to create a one- or two-dimensional matrix in Matlab.
Things You'll Need
- Matlab R2007a or later version
Type the variable name you would like to call your matrix, followed by an equals (=) sign. For example, "x =."
Type a single open square bracket ("[") to start your matrix.
Next, type the numerical data you want to put in your matrix. Separate each number with a single space. For example, "1 2 3."
Type a semicolon and another string of numbers if you wish to create a two-dimensional matrix. For example, "1 2 3; 4 5 6." Otherwise, move on to Step 6.
Type a closed square bracket followed by a semicolon ("];") to complete your line of code and press "Enter."
Tips & Warnings
- If you want to create a one-dimensional matrix, your finished line of code should look something like this: "x = [1 2 3];." You may use different numbers or more numbers if you wish.
- If you want to create a two-dimensional matrix, your finished line of code should look something like this: "x = [1 2 3; 4 5 6];." You may use different numbers or more numbers if you wish.
- If you create a two-dimensional matrix, all the rows must have the same number of numbers. Otherwise, errors will result.
- Remember to put a semicolon at the end of every complete line of code in Matlab. Otherwise, errors will result.
- Photo Credit Hemera Technologies/AbleStock.com/Getty Images