two dimensional array matlab
Matlab has the power and ability to easily compute two, three, and higher
dimensional arrays. In the below examples, neither linspace nor the colon operator can be used
to construct higher dimensional arrays. But the rand can do the work. For example,
A = rand(3,5)
The shape of two dimensional arrays can be seen by how many rows and columns it has. Matlab
stores array dimensions and array number rows and columns. To find the shape of any array, the
size function can do the work.
Retrieving a single entry from a two dimensional array uses the () operator as
well, but with two arguments, the desired row and column index.
A = magic(4) % Create a 4x4 magic square
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
fetches the entry in the first row, second column
A(1,2)
ans =
2
As with one dimensional arrays, user can also retrieve multiple entries in a two
dimensional array at once. For example, to get the second row of the array A above, use the :
operator.
A(2,:)
ans =
5 11 10 8
To obtain rows 1 and 3 at the same time, user can use integer arrays in exactly the same way
that user used them in the vector case.
A([1 3],:)
ans =
16 2 3 13
9 7 6 12
The : operator by itself can be used to retrieve the entire array as one long vector. When used
with row vectors, it has the extra effect of converting the array to a column vector.
A(:)
ans =
16
5
9
4
2
11
7
14
3
10
6
15
13
8
12
1.