Vous êtes sur la page 1sur 8

ENG EK 127

Worksheet 2A

Name: Yen Pham


Section: C2

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 =

0.2785 0.1576 0.8003 0.7922


0.5469 0.9706 0.1419 0.9595
0.9575 0.9572 0.4218 0.6557
0.9649 0.4854 0.9157 0.0357
 5 x 2 matrix of random integers, each in the range from 0 to 100
>>
randi([0,100],5,2)

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

3) Use the equality operator to verify that 3*10^5 is equal to 3e5.


>> 3*10^5==3e5

ans =

1
4) Use the equality operator to verify the value of log10(10000).
>> 4==log10(10000)

ans =

5) Create a matrix variable “mymat” which stores the following:


mymat =
2 5 8
7 5 3
>> mymat=[2:3:8;7:-2:3]

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

-3.1416 -2.8109 -2.4802 -2.1495 -1.8188 -1.4881

Columns 7 through 12

-1.1574 -0.8267 -0.4960 -0.1653 0.1653 0.4960

Columns 13 through 18

0.8267 1.1574 1.4881 1.8188 2.1495 2.4802

Columns 19 through 20

2.8109 3.1416

>> y=sin(x)

y=

Columns 1 through 6

-0.0000 -0.3247 -0.6142 -0.8372 -0.9694 -0.9966

Columns 7 through 12

-0.9158 -0.7357 -0.4759 -0.1646 0.1646 0.4759

Columns 13 through 18

0.7357 0.9158 0.9966 0.9694 0.8372 0.6142

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 *

2.0160 0.0090 0.0120 0.0150 0.0370 0.0534

>> today=myc(1:3)

today =

2016 9 12

>> now=myc(4:6)

now =

15.0000 37.0000 53.3754

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 =

101 139 105


75 120 146

>> flip(mat)

ans =

101 139 105


75 120 146

>> rot90(mat)
ans =

146 105
120 139
75 101

>> reshape(mat,1,6)

ans =

75 101 120 139 146 105

>> 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

9) Find the sum 3+5+7+9+11.


>> sum(3:2:11)

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

>> %'repmat(A,n,m)' replicate the entire array to


% create a new matrix consisting n-by-m tiling of copies of
A
>> % 'repelem(A,n,m) replicates A to create a new matrix
%containing a n-by-m block of each element of A
11) Are there equivalents to intmin and intmax for real number types? Use help to find
out.

Yes. There is a equivalent to intmin and intmax. That is, intmin(‘int32’) and
intmax(‘int32’) respectively are the same as intmin and intmax.