MATLAB Brackets

  1. Use the Square Brackets to Create Vectors or Matrices in MATLAB
  2. Use the Curly Brackets to Create Cell Arrays in MATLAB

In this tutorial, we will discuss how to use the square brackets to create matrices and curly brackets to create cell arrays in MATLAB.

Use the Square Brackets to Create Vectors or Matrices in MATLAB

You can use the square brackets to create vectors and matrices of any size in MATLAB. For example, let’s create a vector and a matrix of integers and a matrix of character array using the square brackets. See the code below.

clc
myVector = [1 2 3]
myMatrix = [1 2 3; 5 6 7]
char_matrix = ['a';'b';'c';'d';'f']

Output:

myVector =

     1     2     3


myMatrix =

     1     2     3
     5     6     7


char_matrix =

  5×1 char array

    'a'
    'b'
    'c'
    'd'
    'f'

We created a vector and a matrix of integers and a matrix of the character array. Each element is saved on a specific index in the matrix. You can extract or change an element present in the matrix using the index of that matrix. Index starts from 1 in MATLAB. For example, let’s get the value present at index 1 in the myVector and change the value present at index 2 in the myVector with the extracted value. See the code below.

clc
myVector = [3 2 6]
value = myVector(1)
myVector(2) = value

Output:

myVector =

     3     2     6


value =

     3


myVector =

     3     3     6

We extracted the value present at index 1, which is 3, and saved it at index 2 in myVector. As you can see, the elements of the vector have been changed. You can do that with a matrix, but you have to give two values now for row and one for column instead of one index. The ones() function to create a matrix of ones and the zeros() function to create a matrix of zeros. The size() function to get the size of a matrix and the find() function to find some specific values present in the matrix. Check this link for more information about matrices.

Use the Curly Brackets to Create Cell Arrays in MATLAB

You can use the curly brackets to create cell arrays of any data type in MATLAB. You can store multiple data types in a cell array. For example, let’s store an integer and a character in a cell. See the code below.

clc
myCell = {10,'Hello'}

Output:

myCell =

  1×2 cell array

    {[10]}    {'Hello'}

The cell array contains two arrays one array contains the integer, and the other contains the character. You can extract and save data into the existing cell using the index and assignment operator. For example, let’s extract the value stored at index 2 and store it at index 3 in the cell. See the below code.

clc
myCell = {10,'Hello'}
v = myCell(2)
myCell(3) = v

Output:

myCell =

  1×2 cell array

    {[10]}    {'Hello'}


v =

  1×1 cell array

    {'Hello'}


myCell =

  1×3 cell array

    {[10]}    {'Hello'}    {'Hello'}

We extracted the value at index 2 and stored it in the variable v, and stored this value at index 3 of the cell array. You can use many cell functions like the cell2mat() to convert a cell array into a matrix and many more functions related to a cell. Check this link for more details about the cell arrays.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - MATLAB Matrix

  • MATLAB Diagonal Matrix