You are on page 1of 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

each in the range from 0 to 100 >> randi([0.100].10) ans = 1 2 3 4 5 6 7 8 9 10 >> 2:5:12 ans = 2 7 12 >> linspace(2.12. >> 3*10^5==3e5 ans = 1 .  5 x 2 matrix of random integers.10.2) ans = 68 96 66 35 17 59 12 23 50 76 2) Create the following vectors twice.3) ans = 2 7 12 3) Use the equality operator to verify that 3*10^5 is equal to 3e5.5. 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.

3. 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.4) Use the equality operator to verify the value of log10(10000).7:-2:3] mymat = 2 5 8 7 5 3 Using this matrix. >> 4==log10(10000) ans = 1 5) Create a matrix variable “mymat” which stores the following: mymat = 2 5 8 7 5 3 >> mymat=[2:3:8.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.2) ans = 2 5 8 2 5 8 7 5 3 7 5 3 .1.

1653 0.9694 0.4802 -2.0000 7) The built-in function clock returns a vector that contains 6 elements: the first three are the current date (year.6) Create a vector x which consists of 20 equally spaced points in the range from – to +.8188 -1.1416 -2.1646 0. but all others are integers.0000 -0.1653 0.20) x = Columns 1 through 6 -3.7357 0. store the first three elements from this variable in a variable “today” and the last three elements in a variable “now”.pi. >> myc=clock . The seconds is a real number.8188 2.4759 Columns 13 through 18 0.9158 -0.8109 3.1646 0. Create a y vector which is sin(x).8372 0. >> x=linspace(-pi.4881 1.4960 Columns 13 through 18 0.4881 Columns 7 through 12 -1. and seconds.3247 -0.9966 Columns 7 through 12 -0.9694 -0.1416 >> y=sin(x) y= Columns 1 through 6 -0. Store the result from clock in a variable called “myc”. Then.8109 -2.7357 -0.8267 1.6142 -0.8267 -0. Use the fix function on the vector variable “now” to get just the integer part of the current time.8372 -0.1574 1.6142 Columns 19 through 20 0.1495 2.9158 0. month.3247 0. minutes.1574 -0.1495 -1.4759 -0. day) and the last three represent the current time in hours.4802 Columns 19 through 20 2.9966 0.4960 -0.

and rot90. flipud.0090 0.3754 8) Create a 2 x 3 matrix variable mat.0000 37.0120 0.0150 0.0e+03 * 2.0370 0. In how many different ways can you reshape it? >> mat=randi([50.0000 53.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) .myc = 1. Pass this matrix variable to each of the following functions and make sure you understand the result: fliplr.2.150]. flip.0534 >> today=myc(1:3) today = 2016 9 12 >> now=myc(4:6) now = 15.0160 0.

3.120].2) ans = .1) ans = 75 101 120 139 146 105 >> reshape(mat.6. Pass it to the repelem function and to the repmat function and make sure you understand the difference.3.6) ans = 75 101 120 139 146 105 >> reshape(mat.1.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.4) mat = 54 79 70 119 78 96 80 61 87 94 51 57 >> repmat(mat. >> mat=randi([50.ans = 146 105 120 139 75 101 >> reshape(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. . That is.1.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.n.m)' replicate the entire array to % create a new matrix consisting n-by-m tiling of copies of A >> % 'repelem(A. 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. intmin(‘int32’) and intmax(‘int32’) respectively are the same as intmin and intmax. Yes. There is a equivalent to intmin and intmax.