You are on page 1of 5

MATLAB CODING

For entering matrices in MATLAB there are several modes:


A=[1 2 3; 2 4 6] a matrix with 2 rows and 3 columns with 1 2 3 on the first row,2 4 6 on the second .
If you write A=[1,2,3,4,5,6] or A=[1 2 3 4 5 6]it will reside in a vector with 6 elements.
A A transpose
For A= [1, 2, 3, 4] size (A) will give 1 4(one row and 4 columns)
If [r, c] = size (A) then r=1 c=4.
TRIL and TRIU functions
Tril (A) gives the lower triangular matrix derived from the initial matrix.
Triu (A) gives the upper triangular matrix ~.
3 2 2
For example if A=1 2 1 tril (A) gives 0 above the main diagonal.
2 4 1

OPERATIONS ON ARRAY

1) Max (A), Min (A) computes as its name says the maximum of the array A and its minimum
respectively.
2) Sort (A) sorts a vector. You can sort the vector in ascending order or descending order.
E.g. A= [1 -1 2 -2] the code sort (A, descend) gives [2 1 -1 -2]
Exercise: Sort a vector until the middle of it in ascending order, and then in descending order the
remaining part.
Response:
e.g. b= [1 -9 7 2 8 5 10 -1 9 3];
The code z=[sort(b(1:(length(b)/2))),sort(b(length(b)/2+1:length(b)),'descend')] gives
Z = -9

8 10

3 -1

3) Adding a number to an array by <b+ value> increases all elements by that value.
Here b is the previous vector. Mean (b) gives the arithmetic mean, std (b) gives the standard deviation
and sum(b) the sum of all elements.
4) [c, I]=max (b) returns the maximum of the array and its corresponding index.

MATRIX OPERATIONS
Saddle points in matrices:
Lets take for example a payoff matrix resulted from actions of two players in a two-player game:
If we look for a saddle point then we seek if min(max(A))=max(min(A))
If not, we dont have any, if we have then the index is found by the following lines:
1. [b, I]=max(A) // This indicates the position of the maximum in every column.
If I= 2 4 3 1 that means on the first column the maximum is attained on the 2nd row, on the 2nd column
the maximum is attained on the 4th row,etc.
2. run again [b, I]=min(max(A)).That will give you the position in which the minimum is attained
from the maximums shown before. If I=1 that means you have the max(min ) value on the 1st
column and the 2nd row, because from 1) I[1]=2.

Now you do the same for min(A),max(min(A)).


If the index is the same, that index is the position of the saddle point.

Exercises (short coding):


1) Select the nonnull elements of a vector and find the arithmetic mean of them. Compare the
means obtained by eliminating the zeros and by not eliminating them.
R: mean(x(find(x))) and mean(x(find(x)))-mean(x)

2) Find the non-null elements of a matrix A, and the nonnull elements of the diagonal, and the
third column. Count them.
R: find(A),find(diag(A)), find(A(:,3)), and for counting them we use length(find(A))
3) Count how many elements are bigger than a value
Solution: length(find(A>value))
4) Find a way to print out the following triangle
1
1

Solution: S=cell(1,4)
S(1:4)={[1],[1 2],[1 2 3],[1 2 3 4]}
For i=1:4
Disp(S{i}),end.
5. Add to a matrix A a specified number of rows equal to [1 1 1 1]
Solution: After declaring A, we write n=the number desired and
For I = 1:n
A[I + size(A,1),:]=[1 1 1 1]
End.
6. a) Delete a row from the matrix: Response:
A (no,: )=[];
b) Delete more consecutive rows from matrix:
A (no1:no2,: )=[]
c) Delete a selection of rows from a matrix:
E.g. deleting rows 3 and 5 from matrix.
A ([3, 5], : ) = []
d) Delete a selection of columns from a matrix: A(:, [3,5])=[] for example.
7. Print the following output:
1 2
2 3

2 1
Solution: S=cell (3, 1);
S (1:3) = {[1 2];[2 3 4];[2 1]}
For i=1:3
Disp (S {i}), end
8 . Write to a txt file a table containing this information:
29,30,32
33,33,100,91
43,44,91

R: S=cell(3,1);
S(1:3)={[29 30 32];[33 33 100 91];[43 44 91]}
T=table(S);
Writetable(T,text.txt,WriteVariableNames,false)

SCRIPTS and FUNCTIONS


1. Sum of two numbers:
We create a file scipt called: sum.m
prompt1 = Give the first number ;
x= input(prompt1);
prompt2 = Give the 2nd number :
y= input(prompt2);
s=x+y;
disp(The sum of the two numbers is: ),disp(s);
then from the command line type <sum>
2. Triangle area
function a=triarea(b,h)
a=0.5*(b.*h);
And you appeal from the command line triarea(10,4) or a=triarea(10,4) etc.
3. Function with multiple outputs
create a function that computes the area and perimeter of a rectangle:
function [a,p]= rectangle(l1,l2)
a=l1*l2;
p=2*(l1+l2)
end
from the command line type [area,perimeter]=rectangle(10,9) e.g. or whatever you want.
4. Nested functions
function [m,s] = stat2(x)
n = length(x);
m = avg(x,n);
s = sqrt(sum((x-m).^2/n));

end
function m = avg(x,n)
m = sum(x)/n;
end

You can type after that , a set of values


values=[11 21 22 21 31]
and [average,stdev]=stat(values)

5. recursive function product


function prod = product(n)
temp=1;
if(n>1)
temp = n * product(n-1);
end
prod=temp;
end

type product(5) to see what happens.


Q: What happens if I change temp=1 with temp=0?
The value retained by the function product at first is 0,and then changes as the function is
sequentially executed.