MATLAB TUTORIAL - PART V
In this part of the tutorial, we will cover how to add or remove elements from already defined variable.
Given a vetor of length n, assigning an element to this vector at position n+3, will add the element at that location and fill n+1 and n+2 with zeros. below is a small example showing what I mean:
>> V=[2 3]
V =
2 3
>> v(4:6)=3:3:9
v =
2 3 5 3 6 9
>> v(10)=6
v =
Columns 1 through 9
2 3 5 3 6 9 0 0 0
Column 10
6
You can also append a vector to another vector as in the following:
>> A=[12 4 5];B=[1:2:6];
>> C=[A B]
C =
12 4 5 1 3 5
Adding elements to a matrix follow the same logic using the addressing techniques that we learned in the previous part of the tutorial, but here we can’t elements, we have to add new rows or new columns or both.
>> M=[1 3 4 5;3 2 3 5]
M =
1 3 4 5
3 2 3 5
>> M(4,:)=[4 4 4 4]
M =
1 3 4 5
3 2 3 5
0 0 0 0
4 4 4 4
Note that assigning a new value to an element beyond the matrix size, causes MATLAB to increase the matrix size in order to include the new element by assigning zeros to the other newly added elements
Also we can append a matrix to another one as in the following example:
M =
1 3 4 5
3 2 3 5
0 0 0 0
4 4 4 4
>> K=ones(4,2)
K =
1 1
1 1
1 1
1 1
>> Z=[M K]
Z =
1 3 4 5 1 1
3 2 3 5 1 1
0 0 0 0 1 1
4 4 4 4 1 1
As we saw, adding new elements is an easy task, and same thing can be done to decrease matrix or vector sizes by removing elements, and this is done by using empty brackets []. The example below will show clearly what I am talking about:
U =
3 55 33 95 55
>> U(2)=[]
U =
3 33 95 55
>> U(2:4)=[]
U =
3
Z =
1 3 4 5 1 1
3 2 3 5 1 1
0 0 0 0 1 1
4 4 4 4 1 1
>> Z(:,[2,3,5:6])=[]
Z =
1 5
3 5
0 0
4 4
Of course when removing elements with matrices, we have to examine the dimension properties like you can’t remove only 1 element from a matrix, but a row or a column or several of these can be removed.
Now since we have a good knowledge about arrays and how to manipulate them, learning some functions might be very useful too:
- length(V) return number of elements of a vector
- size(M) return 2 elements which are the number of rows and number of columns of matrix M
- reshape(A,m,n) rearranges matrix A that has k rows and l columns to have m rows and n columns. k*l=m*n
>> A=[ 2 3 4 ;6 5 77]
A =
2 3 4
6 5 77
>> B=reshape(A,3,2)
B =
2 5
6 4
3 77
- diag(v) creates a square matrix where the elements on the diagonal are the elements of the vector v
- diag(A) returns the diagonal elements of matrix A
We will not go in details about Strings in MATLAB, but we will mention that they are also an array of charaters, and they are created by typing characters within single quotes. The string can contain letters, digits, spaces, or any other symbol example ‘fd g123%’
If a string has a single quote in it, we can do that by typing two single quotes within the string.
We have to note that characters of a string are stored in an array just like number, and a one line string is nothing but a row vector.
To place a string in a matrix, all what we have to do is to use a semicolon as in the following:
>> str=’fdf% 6′
str =
fdf% 6
>> mat=[str;'fjg$4 ']
mat =
fdf% 6
fjg$4
It is a good idea if you consult MATLAB’s help for see the funtion ‘char’.
In the next part of the tutorial, we will deal more with arrays, and complex mathematical operations, with some graphing techniques. Click below to view the next tutorial:
Filed under: MATLAB

Leave a Reply
You must be logged in to post a comment.