Find a Guide

Arrays in MATLAB

Introduction

In MATLAB, we can save values - strings and numbers - in a value, however, what if we want to save multiple values together in one variable? In this case, we would be creating an array in MATLAB! An array is a 1D, or 1-dimensional, variable that allows us to add, edit, and remove values. Let's take a look at how to start defining some arrays.

Defining Arrays

To define an array in MATLAB, we can simply use the square brackets ( [ ] ) to wrap our values like below:

matlab

arr1 = [3 4 7 8];

arr2 = [3, 4, 7, 8];

When we define an array, notice how we can separate our value with either a space or a comma; MATLAB reads both cases as the same, so use whatever is best for your programming needs!

Now what happens if we define an array with mixed values? Great question! Let's try it.

matlab

arr3 = [1 "hello" true 'characters'];
% output: 1x4 string
%               "1" "hello" "true" "characters"

arr4 = [3 false 'char'];
% output: This will error because there is a character array and boolean
%.              -> MATLAB can't convert a boolean to a char array

arr5 = [4 'char2'];
% output: 'char2'

We have some interesting examples to unpack; let's start with the first array, arr3. In this array, we have a number, a string, a boolean, and a character array. What is interesting is if there is a string present in an array, then MATLAB will convert all values in that array to be strings. That is why we get a 1x4 string array as our output.

For arr4, if we tried running that in MATLAB, we would actually get an error because MATLAB cannot convert a boolean variable into a character array. For arr5, we see this in action where MATLAB converts the array to just be a character array of 'char2'.

What we can take from this is that it's important to always test how MATLAB behaves with multi-type arrays.

Column Arrays

There are two different types of arrays: row and column arrays. The default array type is a row array, but now we will learn about how to make column arrays. Let’s see how to define a column array.

matlab

colArr = [1;2;3;4]; 
% output: 1
		  2
		  3
  		  4

Unlike the row arrays, column arrays must use only a semicolon between data points to start a new row in the column. There is also a notation associated with arrays and matrices where they are called by the number of rows followed by the number of columns. For example, we would call colArr a 4 by 1, or 4x1 array because there are 4 rows and 1 column. We will touch on this much more when we get to matrices.

Now you may be wondering what happens if we use both commas and semicolons to define rows and columns. Well, then we get a matrix, but there will be more on that later!

Array Builders

There are however several array builders in MATLAB. Let's first talk about the colon operator. The colon operator has the structure of 'start:step:end'. Let's see this in action.

matlab

arr6 = 1:2:5;
% output: 1 3 5

arr7 = 5:-1:2;
% output: 5 4 3 2

arr8 = 8:10;
% output: 8 9 10

In the code above, arr6 is starting at 1 and incrementing by 2 until it reaches 5, hence, why the array's values are 1, 3, and 5. You can also step backwards too by using negative numbers. This is what we did in arr7 to count backwards.

For arr8, notice we only have a start and an end input, we do not have a step defined. That is ok! If no step is provided, then the default will be 1.

Now let's look at linspace and how it works. linspace is a built-in function with 3 inputs: start, stop, and the number of increments in the array, including the start and stop values.

matlab

arr9 = linspace(1,2,3);
% output: 1 1.5 3

arr10 = linspace(10,8,4);
% output: 10 9.333 8.667 

With linspace, it makes array creation very easy if we know our bounds and the number of total values in the array. Notice how with arr10, we can also count backwards by defining our start value as a number larger than our end value and then defining the number of array values.

Editing Arrays

Now that we have learned how to define arrays, let's learn how to edit them. To access an index or indices of an array, we can simply type the array title followed by a pair of parenthesis that contain an array index as so:

matlab

% From before
% arr2 = [3, 4, 7, 8]
arr2(2) = 21
% output: 3 21 7 8

All MATLAB array indices start at the index of 1, so the first value would have an index of 1, the second would have an index of 2, etc. We can also change multiple values by using the colon operator as well.

matlab

% From before
% arr2 = [3, 21, 7, 8]
arr2(3:4) = [1, 89]
% output: 3 21 1 89
  • Notice since we replaced multiple values within our array (ex, the '3:4'), we had to use an array of values as our replacement (ex, the '[1, 89]').

    Now what if we want to target the last value in an array? Well, we could do this using 2 different ways: either using the length() function or by using the 'end' key.

matlab

% From before
% arr2 = [3, 21, 7, 8]

arr2_len = length(arr2)
% output: 4

arr2(arr2_len)
% output: 8

arr2(end)
% output: 8

In the above code, we first get the length of arr2 by using the length() function. This returns the numerical length of an array, hence, why we got a length of 4 for arr2. We then used that length as a way to get the last value in the MATLAB array. Notice how we got the last value using the 'end' key as well. Both work!

Concatenation

Now let's look at how we can concatenate two arrays, or add a value within an array.

matlab

arr11 = [1 2 3];
arr12 = [5 6 7];

arr13 = [arr11 4 arr12]
% output: 1 2 3 4 5 6 7

Let's say we tried making an array that spans from 1 to 7, but we made it in two parts and forgot the number '4'! We can use, what's called, array concatenation to combine two or more arrays and values together to make one larger array. All we have to do is list out our arrays and values in between square brackets, like above. Then we can make an assembled array!

Character Arrays

Now we have an understanding of arrays, character arrays start to make more sense of how they are different than strings. Let’s try defining an array of character arrays and see what happens.

matlab

charArr = [‘hello’ ‘world’]; % output ‘helloworld’

length(charArr); % output: 10

charArr(2) = ‘P’; % output: ‘hPlloworld’

As we can see, character arrays can be treated like typical arrays and can be modified as well.

Hermitian w/ Arrays

I would be remise if I did not talk about the Hermitian, or apostrophe, operator in MATLAB. Let’s say the colon method is used to create a row array, but we would like the array to be a column array. This is where the Hermitian comes into play. The Hermitian is synonymous to taking the transpose of a matrix or array if the array is real. If the array is not real and has imaginary values, then the complex conjugate will be taken and then the matrix/array will be transposed. Let's see what I mean by this:

matlab

arrRow = 1:4; % output: 1 2 3 4

arrCol = arrRow’;
% output: 1
		  2
		  3
  		  4

As we can see above, the apostrophe transposes arrRow into a column array, arrCol. This is a very useful operator, especially when doing matrix multiplication to make sure the dimensions of the matrices are correct.

Find a Guide