You are on page 1of 11

New commands and functions

• rand
>> r = rand(n) returns an n-by-n matrix containing pseudorandom
values drawn from the standard uniform distribution on the open
interval (0,1).
>> r = rand(m,n) or r = rand([m,n]) returns an m-by-n
matrix.
>> r = rand(m,n,p,...) or r = rand([m,n,p,...]) returns
an m-by-n-by-p-by-... array.
>> r = rand returns a scalar.
• size
>> d = size(X) returns the sizes of each dimension of array X in a
vector d. If X is a scalar, then size(X) returns the vector [1, 1].
>> [m,n] = size(X) returns the size of matrix X in separate
variables m and n.
>> m = size(X,dim) returns the size of the dimension of X specified
by scalar dim.
>> [d1,d2,d3,...,dn] = size(X) for n>1, returns the sizes of
the dimensions of the array X in the variables d1,d2,d3,...,dn.
New commands and functions
• Max
>> C = max(A)
If A is a vector, max(A) returns the largest element in A.
If A is a matrix, max(A) treats the columns of A as vectors, returning a
row vector containing the maximum element from each column.
>> [C,I] = max(A)
finds the indices of the maximum values of A, and returns them in
output vector C, and their indexes in the vector I. If there are several
identical maximum values, the index of the first one found is returned.
• Min
>> C = min(A)
If A is a vector, min(A) returns the smallest element in A.
If A is a matrix, min(A) treats the columns of A as vectors, returning a
row vector containing the minimum element from each column.
>> [C,I] = min(A)
finds the indices of the minimum values of A, and returns them in
output vector C, and their indexes in the vector I. If there are several
identical minimum values, the index of the first one found is returned.
New commands and functions
• length
>> n = length(array)
Finds the number of elements along the largest dimension of an
array.
For nonempty arrays, n is equivalent to max(size(array)).
For empty arrays, n is zero.
>> a=[3 5];
>> b = rand(4)
b =
0.6557 0.6787 0.6555 0.2769
0.0357 0.7577 0.1712 0.0462
0.8491 0.7431 0.7060 0.0971
0.9340 0.3922 0.0318 0.8235
>> c = rand(a)
c =
0.6948 0.0344 0.7655 0.4898 0.7094
0.3171 0.4387 0.7952 0.4456 0.7547
0.9502 0.3816 0.1869 0.6463 0.2760
>> d = round(rand(a)*100)
d =
68 12 34 75 70
66 50 59 26 89
16 96 22 51 96
>> e = size(d) d =
e = 68 12 34 75 70
66 50 59 26 89
3 5
16 96 22 51 96
>> [f g] = size(d)
f =
3
g =
5
>> h = size(d,2)
h =
5
>> i = length(d)
i =
5
>> j = length(d')
j =
5
>> k = ones(size(d))./2 d =
68 12 34 75 70
k =
66 50 59 26 89
0.5 0.5 0.5 0.5 0.5 16 96 22 51 96
0.5 0.5 0.5 0.5 0.5
0.5 0.5 0.5 0.5 0.5

>> e = d(:,[1 3 5])


e =
68 34 70
66 59 89
16 22 96

>> e(:,3) = e(:,3) + e(:,1)*100


e =
68 34 6870
66 59 6689
16 22 1696
>> ones = zeros(1,length(d))+1 d =
68 12 34 75 70
ones =
66 50 59 26 89
1 1 1 1 1 16 96 22 51 96

>> ones(3,4)
Index exceeds matrix dimensions.

>> max(d)
ans =
68 96 59 75 96

>> min(d')'
ans =
12
26
16
>> [val , pos] = min(d) d =
68 12 34 75 70
val =
66 50 59 26 89
16 12 22 26 70 16 96 22 51 96
pos =
3 1 3 2 1

>> [v p] = max(d(2,:))
val =
89
pos =
5
Solve the follwing equations using MATLAB

2x + y = 9
3x – y = 16
>> a = [ 2 1 ; 3 -1 ]
a=
2 1
3 -1

>> b = [ 9 ; 16 ]
b=
9
16

>> sol = a\b


sol =
5.0000 ← x
-1.0000 ← y

>> sol = b/a


Error using /
Matrix dimensions must agree.
Exercise 1: Solve the follwing equations using MATLAB
5x + 3z – 7y = 15
8x + 1z – 6y = -8
4z + 7y = 7
>> a = [ 5 3 -7 ; 8 1 -6 ; 0 4 7 ]
a =
5 3 -7
8 1 -6
0 4 7

>> b = [15 ; -8 ; 7]
b =
15
-8
7

>> a\b
ans =
-3.2911 ← x
5.4937 ← z
-2.1392 ← y
Exercise 2: Create a 4x6 array that contain random numbers between -50 and 50 then find
the smallet element in that array.

>> rand(4,6)*100-50
ans =
20.9365 15.5098 45.9744 25.1267 39.0903 -35.0706
25.4687 -33.7388 -15.9614 -24.4905 45.9291 -24.2492
-22.3975 -38.1002 8.5268 0.5957 4.7216 34.0717
17.9703 -0.1636 -27.6188 19.9077 -36.1376 -24.5718

>> minimum = min(min(ans))


minimum =
-38.1002

You might also like