How to Create a Matrix, Vector, and Cell Array in MATLAB
Part 1 of 4:
Creating Matrices
- Create a matrix. A matrix is a rectangular set of numbers that are arranged in multiple rows and columns. Code a matrix by listing a series numbers separated by spaces or commas per row within the command window. For a new row, a semi colon is placed between the values of the old row and new row. The matrix is finished by surrounding it by brackets. Hit enter when finished to display the matrix.
- Example: M=[2,3,4,5;6,7,8,9] or M=[2 3 4 5;6 7 8 9]
- Example: M=[2,3,4,5;6,7,8,9] or M=[2 3 4 5;6 7 8 9]
- Create a one value matrix. Type commands such as zeros or ones to create a matrix with only one value within it. Follow these commands by the number of rows then the number of columns separated by a comma and surrounded by parenthesis.
- Example: zeros(# of rows, # of columns)
- Example: ones(# of rows, # of columns)
- Edit a ones matrix. You can multiply a ones matrix by any number to create a new matrix in which all the elements are the value which the ones matrix was multiplied by.
- Example: zeros(# of rows, # of columns)
Part 2 of 4:
Creating Vectors
- Create a horizontal vector. A vector is a set of numbers that are arranged in a single row or single column. Code a horizontal vector by listing a series of numbers in the vector that are separated by spaces or commas and surrounded by brackets. Hit enter once completed the code to display the vector.
- Example: V=[2,3,4,5,6,7,8,9] or V=[2 3 4 5 6 7 8 9]
- Example: V=[2,3,4,5,6,7,8,9] or V=[2 3 4 5 6 7 8 9]
- Create a vertical vector. Start by listing a series of numbers that are separated by semicolons and surrounded by brackets. Hit enter once completed the code to display the vector.
- Example: V=[2;3;4;5;6;7;8;9]
- Example: V=[2;3;4;5;6;7;8;9]
- Create a one value vector. Type commands such as zeros or ones to create a horizontal or vertical vector with only one value within it. Follow these commands by the number of rows then the number of columns separated by a comma and surrounded by parenthesis.
- Example: zeros(# of rows, # of columns)
- Example: ones(# of rows, # of columns)
- Edit a ones vector. You can multiply a ones vector by any number to create a new vector in which all the elements are the value which the ones vector was multiplied by.
- Example: zeros(# of rows, # of columns)
- Create a vector with a pattern. A pattern is a list of numbers that follow a certain rule or sequence. Type the colon operator or the linspace command. A colon operator focuses on a specific increment that the pattern follows while the linspace command focuses on the number of variables within the pattern that are have the same increment between them.
- Use a colon operator. Type the first number of pattern, increment between the values, and last number of the pattern all separated by colons. Hit enter once code is completed.
- Use linspace command. Type linspace then first number in pattern, last number in the pattern, total number of equally spaced values separated by commas and surrounded by parenthesis. Hit enter once the code is completed.
- Use a colon operator. Type the first number of pattern, increment between the values, and last number of the pattern all separated by colons. Hit enter once code is completed.
Part 3 of 4:
Creating Cell Arrays
- Create a cell array. A cell array is a rectangular set of data similar to a matrix but it can hold any type of data such as text, numbers, and/or vector. Code a cell array by listing a series of numbers, vectors, or characters in the same format as a matrix while characters are in quotation marks and vectors are in brackets. The cell array is surrounded by curly brackets { }. Hit enter once the code is completed.
- Example: C={1, 'h', 5, 7; [1:2:11], 'm', 8, 25} or C={1 'h' 5 7; [1:2:11] 'm' 8 25}
- Example: C={1, 'h', 5, 7; [1:2:11], 'm', 8, 25} or C={1 'h' 5 7; [1:2:11] 'm' 8 25}
- Double-click on the cell array's value in the workspace to view the cell array entirely. A new window will open to so you can confirm your vector entry in the cell array.
Part 4 of 4:
Avoiding Common Errors
- Ensure that all commands are in lowercase letters.
- Ensure that the Matrix has the same number of variables in each column.
- Ensure that the Matrix has the same number of variables in each row.
-
- Ensure that the Cell Array has curly brackets { } enclosing it.
- Ensure that the Matrix and/or Vector has brackets [ ] enclosing it
-
- Ensure that the words or letters within a cell array are in quotation marks.
5 ★ | 1 Vote
You should read it
- How to Use MatLab to Solve Matrix Equations and Perform Statistical Analysis
- Matrix Ransomware is back under the distribution of RIG Exploit Kit
- Matrix functions in Excel
- Top 5 best vector image creation software
- MINVERSE function - The function returns the inverse matrix of a given matrix in Excel
- How to draw vector illustrations from A to Z
- How to Convert JPG to Vector
- How to convert to Vector in Photoshop
May be interested
- How to Import, Graph, and Label Excel Data in MATLABwhether you are an experienced matlab user or a novice, you may not be fully aware of matlab's graphing abilities. matlab allows you to easily customize, label, and analyze graphs, giving you more freedom than the traditional excel graph....
- How to Think Like a Programmerthinking like a programmer primarily means being persistent and meticulous in your attention to details. jump into programming challenges feet-first, but review your work carefully as you proceed to ensure you don't end up with any bugs in...
- How to Convert Odt to Wordfiles that end with the extension 'odt' are from the program 'open office.org' or libreoffice. if you have word 2010 or 2013, you can open an odt file simply by double-clicking it. if you're using an older version of word, or you are using...
- How to Program with VBScriptvbscript is a windows-native programming language that is mainly used to create web server applications. vbscript is embedded within an html file, and is fairly straightforward. note that vbscript differs from visual basic, which is used...
- How to Create a Print Preview Control in Visual Basiccreating a windows application and need to know how to create a print and print preview control? follow these simple steps and you will easily be able to print and print preview documents in your application. adjust the form to the size...
- How to Protect VBA Codemicrosoft's visual basic for applications (vba) is the standard language for writing programs to automate functions and tasks in microsoft office. understand how to protect vba code so others can't sabotage or steal your macros. open the...