Worksheet 2A
Instructions: The minimum set for this worksheet is all Problems. Note: the important
thing to record is the MATLAB expression(s) that you use to solve these problems.
1) Generate a
random real number in the range from 0 to 1
>> rand
ans =
0.8147
random real number in the range from 20 to 50
>> rand*(50-
20)+20
ans =
47.1738
random integer in the range from 0 to 11
>> randi([0,11])
ans =
3
random integer in the range from 50 to 100
>>
randi([50,100],1)
ans =
54
4 x 4 matrix of random real numbers, each in the range from 0 to 1
>> rand(4)
ans =
ans =
68 96
66 35
17 59
12 23
50 76
2) Create the following vectors twice, using linspace and using the colon operator:
1 2 3 4 5 6 7 8 9 10
2 7 12
Solution:
>> 1:10
ans =
1 2 3 4 5 6 7 8 9
10
>> linspace(1,10,10)
ans =
1 2 3 4 5 6 7 8 9
10
>> 2:5:12
ans =
2 7 12
>> linspace(2,12,3)
ans =
2 7 12
ans =
1
4) Use the equality operator to verify the value of log10(10000).
>> 4==log10(10000)
ans =
mymat =
2 5 8
7 5 3
Using this matrix, find a simple expression that will transform the matrix into each of
the following:
2 7
5 5
8 3
>> mymat'
ans =
2 7
5 5
8 3
2 5
7 8
5 3
>> reshape(mymat,3,2)
ans =
2 5
7 8
5 3
8 5 2
3 5 7
>> fliplr(mymat)
ans =
8 5 2
3 5 7
8 3
5 5
2 7
>> rot90(mymat)
ans =
8 3
5 5
2 7
2 5 8 2 5 8
7 5 3 7 5 3
>> repmat(mymat,1,2)
ans =
2 5 8 2 5 8
7 5 3 7 5 3
6) Create a vector x which consists of 20 equally spaced points in the range from – to
+. Create a y vector which is sin(x).
>> x=linspace(-pi,pi,20)
x =
Columns 1 through 6
Columns 7 through 12
Columns 13 through 18
Columns 19 through 20
2.8109 3.1416
>> y=sin(x)
y=
Columns 1 through 6
Columns 7 through 12
Columns 13 through 18
Columns 19 through 20
0.3247 0.0000
7) The built-in function clock returns a vector that contains 6 elements: the first three are
the current date (year, month, day) and the last three represent the current time in hours,
minutes, and seconds. The seconds is a real number, but all others are integers. Store the
result from clock in a variable called “myc”. Then, store the first three elements from
this variable in a variable “today” and the last three elements in a variable “now”. Use
the fix function on the vector variable “now” to get just the integer part of the current
time.
>> myc=clock
myc =
1.0e+03 *
>> today=myc(1:3)
today =
2016 9 12
>> now=myc(4:6)
now =
8) Create a 2 x 3 matrix variable mat. Pass this matrix variable to each of the following
functions and make sure you understand the result: fliplr, flipud, flip, and rot90. In how
many different ways can you reshape it?
>> mat=randi([50,150],2,3)
mat =
75 120 146
101 139 105
>> fliplr(mat)
ans =
146 120 75
105 139 101
>> flipud(mat)
ans =
>> flip(mat)
ans =
>> rot90(mat)
ans =
146 105
120 139
75 101
>> reshape(mat,1,6)
ans =
>> reshape(mat,6,1)
ans =
75
101
120
139
146
105
>> reshape(mat,3,2)
ans =
75 139
101 146
120 105
>> % We can reshape the matrix 'mat' in 3 different ways
ans =
35
10) Create a matrix variable of random integers. Pass it to the repelem function and to
the repmat function and make sure you understand the difference.
>> mat=randi([50,120],3,4)
mat =
54 79 70 119
78 96 80 61
87 94 51 57
>> repmat(mat,1,2)
ans =
54 79 70 119 54 79 70 119
78 96 80 61 78 96 80 61
87 94 51 57 87 94 51 57
>> repelem(mat,1,2)
ans =
54 54 79 79 70 70 119 119
78 78 96 96 80 80 61 61
87 87 94 94 51 51 57 57
Yes. There is a equivalent to intmin and intmax. That is, intmin(‘int32’) and
intmax(‘int32’) respectively are the same as intmin and intmax.