MATLAB TUTORIAL - PART III

As we have mentioned earlier, Arrays are fundamental forms used to in MATLAB to store and manipulate data.
It is a list of numbers arranged in rows and/or columns. And of course, the simplest array, is a one dimensional array which is know as a Vector.
And a vector can be of 2 forms, either a row vector or a column vector.
We should note that Arrays can handle numbers as well as charaters and strings.

A one dimensional array, can be used for example to represent the position of a point in space.
Of course, there are several ways to create a vector depending on the source of information which can be specific numbers or mathematical expressions or a series of elements with constant spacing.

Below are some of the different forms:

  • If you have a certain number of data like the profit of a company between in years 1988 1989 1990 1991

You can directly represent the info above by:

>> year=[1988 1989 1990 1991]
year =
1988        1989        1990        1991
>> profit=[10000;15000;8000;1000]
profit =
10000
15000
8000
1000

  • Vectors with constant spacing

Vector_name=a:n:b

where a is the first term, n is the spacing , and b is the last term.

If we remove n, the default spacing will be equal to 1. Below is a example on how to use this vector spacing:

>> x=2:2:10
x =
2     4     6     8    10

  • There is also another type of spacing, where you specify the first term, the last term, and how many elements you want in this vector.

Vector_name=linspace(a,b,m) where a is the first term, b is the last term, and m is the number of elements. If m is omitted, the the default number is 100.

>> u=linspace(1,5,6)
u =
1.0000    1.8000    2.6000    3.4000    4.2000    5.0000

When it comes to the Two-dimensional Array, it can be wither a square matrix or a general mxn matrix. And here is how to declare a matrix:

Matrix_name=[1st row;2nd row;...; nth row]

Of course, all the rows must have the same number of elements.

Below are some commands which are very useful in matrix creation:

  • Zeros(m.n) and ones(m,n) are used to create and mxn matrix with elements 0 and 1 respectively
  • eye(n) creates an nxn identity matrix where the diagonal elements are equal to 1

Here are some examples below:

>> a=[1 1 2;23 3 4;8 -9 0]
a =
1     1     2
23     3     4
8    -9     0
>> t=4;y=7;u=9;
>> a2=[t y u;t*u y-u y*t;t/y u y]
a2 =
4.0000    7.0000    9.0000
36.0000   -2.0000   28.0000
0.5714    9.0000    7.0000

>> a3=[1:2:6; 1 3 4;linspace(23,59,3)]
a3 =
1     3     5
1     3     4
23    41    59

>> a4=zeros(2,3)
a4 =
0     0     0
0     0     0
>> a5=eye(3)
a5 =
1     0     0
0     1     0
0     0     1
>> a6=ones(2)
a6 =
1     1
1     1

Just to note that, all the variables in MATLAB are arrays where a scalar variable is a one element array, a vector is an array with one row or colums, matrix is an array with elements in both rows and columns.

The good thing in MATLAB, is that it is easy to change the variable type or size.

In the next part, we will continue with arrays, and mentions some operations and array addressing modes. Click below to view the next tutorial.

Part IV

Leave a Reply

You must be logged in to post a comment.