9.4. Manipulating Arrays#

There may be times when you want to be able to manipulate arrays by performing actions such as appending values to the end of an array, inserting values into the middle and array or deleting elements from an array altogether.


9.4.1. Appending to an array#

To append (put something on the end of) to an array we use the syntax

[ array, object ]

where object is the thing we are appending to array. To demonstrate this lets append the array [4, 5] to the end of the array a. Add the following code to your program.

% Manipulating arrays
a

fprintf("\nAppend [4, 5] to array a:")
a = [a, [4, 5]]

Run the program and you should see the following added to the command window.

a =

     1     2     3


Append [4, 5] to array a:
a =

     1     2     3     4     5

When dealing with 2D arrays we can append to the right or underneath our array. To append to the right we use

array = [array, object]

and to append underneath we use

array = [array ; object]

To demonstrate this, enter the following code into your program.

fprintf("\nAppend B to the bottom of A:")
[A ; B]

fprintf("\nAppend B to the right of A:")
[A , B]

Run your program and you should see the following added to the command window.

Append B to the bottom of A:
ans =

     1     2
     3     4
     5     6
     7     8


Append B to the right of A:
ans =

     1     2     5     6
     3     4     7     8

9.4.2. Inserting into an array#

To insert an object into an array we can use

array = [ array(1:index - 1), object, array(index:end) ]

where object will be inserted into array starting at the index. Lets insert the array [6, 7, 8] starting at the 3rd element of a. Add the following code to your program.

fprintf("\nInsert [6, 7, 8] into the array a:")
a = [a(1 : 2), [6, 7, 8], a(3:end)]

Run the program and you should see the following added to the command window.

Insert [6, 7, 8] into the array a:
a =

     1     2     6     7     8     3     4     5

We can do similar with 2D arrays. To demonstrate this lets insert the rows of B between the rows of A. Enter the following code into your program.

fprintf("\nInsert B between the rows of A:")
[A(1,:) ; B ; A(2,:) ]

Run the program and you should see the following added to the command window.

Insert B between the rows of A:
ans =

     1     2
     5     6
     7     8
     3     4

Lets insert the columns of B between the columns of A. Enter the following code into your program.

fprintf("\nInsert B between the columns of A:")
F = [A(:,1), B, A(:,2) ]

Run the program and you should see the following added to the command window.

Insert B between the columns of A:
ans =

     1     5     6     2
     3     7     8     4

9.4.3. Deleting from an array#

To delete an object or objects from an array we can use the syntax

array(index) = []

where the objects located at index will be removed from array. Lets remove the numbers 7 and 8 from a. We want to delete the elements with indices 4 and 5 so add the following code to your program.

fprintf("\nDelete the 4th and 5th elements of a:")
a(4 : 5) = []

Run the program and you should see the following added to the console.

Delete the 4th and 5th elements of a:
a =

     1     2     6     3     4     5

When dealing with 2D arrays we need to specify the indices for the rows and columns of the elements we want to remove.

C = [1, 2, 3 ; 4, 5, 6 ; 7, 8, 9]

fprintf("\nDelete the 3rd row of C:")
new_matrix = C;
new_matrix(3, :) = []

fprintf("\nDelete the last column of C:")
new_matrix = C;
new_matrix(:, end) = []

Run the program and you should see the following added to the console.

C =

     1     2     3
     4     5     6
     7     8     9


Delete the 3rd row of C:
new_matrix =

     1     2     3
     4     5     6


Delete the last column of C:
new_matrix =

     1     2
     4     5
     7     8

9.4.4. Sorting an array#

To sort an array into ascending order we can use the sort() function. The syntax for this is

sort(array)

Lets sort the array a into ascending order. To demonstrate this, enter the following code into your program.

fprintf("\nSort array a into ascending order:")
a = sort(a)

Run the program and you should see the following added to the command window.

Sort array a into ascending order:
a =

     1     2     3     4     5     6

9.4.5. Reshaping an array#

To reshape an array (change the shape to a different number of rows and columns) we can use the reshape() function. The syntax for this is

reshape(array, rows, columns)

Lets reshape a so that it is a 3 \(\times\) 2 array, enter the following code into your program.

fprintf("\nReshape array a into an 3 x 2 array:")
a_3x2 = reshape(a, 3, 2)

Run the program and you should see the following added to the command window.

Reshape array a into an 3 x 2 array:
a_3x2 =

     1     4
     2     5
     3     6

Here a 2D array is formed by reshaping the 1D array a into a \(3 \times 2\) array where the elements are reshaped using row-major order so the first two elements a form the first row of of the 2D array, the next two elements form the second row of the 2D array and so on.

The other way is to use column-major order where the columns of the reshaped are formed by taking successive elements to from the original array. To do this in MATLAB we simply transpose the array we are reshaping and swap the rows and columns inputs around.

reshape(array', columns, rows)'

So to reshape a so that it is a 3 \(\times\) 2 array using row-major order enter the following code into your program.

fprintf("\nReshape array a into an 3 x 2 array row by row:")
a_3x2 = reshape(a', 2, 3)'

Run the program and you should see the following added to the command window.

Reshape array a into an 3 x 2 array row by row:
a_3x2 =

     1     2
     3     4
     5     6

9.4.6. Exercise#

Exercise 9.4

Use the arrays from Exercise 9.1 and appropriate functions to print the following:

  1. \(D\) appended to the right of \(B\)

  2. The first two rows of \(D\) appended to the bottom of \(B\)

  3. \(D\) inserted between the 2nd and 3rd columns of \(B\)

  4. \(B\) with the middle row removed

  5. \(\vec{a}\) sorted in descending order

  6. \(C\) reshaped into an \(8 \times 1\) array