Section 2.5 Addressing Arrays
We already have experience working with arrays from studying the C programming language. There are some subtle and some not-so-subtle differences with working with arrays in MATLAB, some of them syntactical and others more fundamental. The main differences from C are:
In MATLAB, array elements are addressed using parentheses ()
(and not brackets []
as in C).
In MATLAB you can select or address more than one array element at once and thus perform operations with multiple elements at once. This will become clearer as soon as we look at some examples.
Arrays in MATLAB are ones-based (1-N) rather than zero-based. For example, if A = [5 -2 4 3]
is your array, then the “5” is located at index “1” of the array in MATLAB whereas in C we would use the index “0”. So in MATLAB, A[1] = 5
whereas in C, A(0) = 5
.
Here are some examples of how to find the kth element of a vector “ve” or the element in row k and column p of matrix “ma” in MATLAB. Do you remember how to address the equivalent elements in C?
>> ve(k) % kth element in a vector
>> ma(k,p) % kth row and pth column of matrix
Once you have addressed an array element (and assuming it is a number), we can now use these scalar values just like any other scalar value.
You can also select entire ranges of elements in an array. The colon operator :
is often used for this purpose. Its use is most easily understood by looking at some examples.
Let’s start with using the colon operator to select ranges of elements in a vector. Suppose “ve” is a vector.
>> % vector examples:
>> ve(:) % all elements in a vector
>> ve(p:q) % all elements from index p to index q in a vector
Now let’s look at matrices. Suppose that “ma” is a matrix with “nrow” rows and “ncol” columns.
>> % matrix examples:
>> ma(:,j) % all rows, column j (result is nrow by 1)
>> ma(i,:) % row i, all columns (result is 1 by ncol)
>> ma(:,p:q) % columns p through q
>> ma(p:q,:) % rows p through q
>> ma(p:q,r:s) % rows p to q of cols r to s
Note that when you address multiple elements, the result is a vector or matrix of the selected size:
Activity 2.7.
Suppose that you have typed:
>> u = 0:10
u =
0 1 2 3 4 5 6 7 8 9 10
And now you type:
>> v = u(3:5);
>> w = u([9 1 4 8]);
What values will v
and w
contain?
You can explicitly list the indices, in your desired order, whose corresponding elements you are interested in. Note that these indices are listed inside the parentheses of “u(...)” in the form of a vector, so enclosed in brackets.
The same principles hold for matrices, for example:
>> ma = [0 1 2 3; 4 5 6 7; 8 9 10 11; 12 13 14 15]
ma =
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
>> sa = ma(3:4, 1:2)
sa =
8 9
12 13
Here are some more examples. Suppose we define a matrix A
as follows:
>> A = [2 3 4 5; 1 2 3 4]
A =
2 3 4 5
1 2 3 4
If you wanted to select the first column of this matrix, you’d choose all rows and specify column 1 as is done here:
And if you wanted to select several columns at once, for example columns 1-3 you could do so as follows:
>> A(:, 1:3)
ans =
2 3 4
1 2 3