Next: 4. Linear algebra
Up: 3.2 Manipulating matrices and
Previous: 3.2.4 Special arrays
  Contents
Arrays with multiple dimensions are indexed in the same fashion as
one-dimensional arrays we learned about in
Section 3.1. The index of each dimension is separated by a
comma. For example, the element in the second row, third column of matrix
would be indexed
. Blocks of elements are indexed using the
colon notation, just as before. Do the following examples.
% Accessing elements of multidimension arrays
a = 1:12; % define a 1D array
A = reshape(a,4,3) % reshape turns the elements of 'a' into a 4x3 array
% we didn't have to initialize A this way, it's just
% convenient
A(2,3); % gives 10 (the 6th element of 'A')
A(3:4,2:3) % gives a 2x2 array using the rows 3 & 4 and columns 2 & 3
B = A(:,3:-1:1) % Tricky! How does this work?
B(3,1) = pi % Replace just a single element with pi
MATLAB can handle arrays with any number of dimensions. We will rarely
find a use for arrays with more than 3 dimensions. Two- and one-dimensional
arrays (matrices and vectors) are at the heart of computational physics, so
we'll use these the most.
Gus Hart
2005-01-28