The array is a fundamental form that MATLAB uses to store and manipulate data. An array is a list of numbers arranged in rows and or columns. The simplest array (one- dimensional) is a row, or a column of numbers. A more complex array (two- dimensional) is a collection of number arranged in rows and columns.
Matrix generation
Matrices are fundamental to MATLAB. Matrices can be generated in several ways.
Creating A One – Dimensional Array (Vector)
1. Row Vector
Enter a statement like
>> v = [1 4 7 10 13]
v =
1 4 7 10 13
2. COLUMN VECTORS
Column vectors are created in a similar way, however, semicolon (;) must separate the
Components of a column vector,
>> w = [1;4;7;10;13]
w =
1
4
7
10
13
Initializing vectors: the colon operator
A vector can also be generated (initialized) with the colon operator, Enter the following statements:
x = 1:10
(elements are the integers 1, 2, …, 10);
x = 1:0.5:4
(elements are the values 1, 1.5, …, 4 in increments of 0.5—note that if the colons separate three values, the middle value is the increment);
x = 10:-1:1
(elements are the integers 10, 9, …, 1, since the increment is negative);
x = 1:0
(a complicated way of generating an empty vector!).
Creating a vector with constant spacing
A vector in which the first element is xi, the last element is xf, and the number of elements is n is created by typing the linspace command (MATLAB determines the correct spacing):
|
>> b=linspace(3,13,6) b = 6 elements, first element 3, last element 13. 3 5 7 9 11 13 |
|