lab08_genn004m_vectorizedcode

3
Cairo University Faculty of Engineering Credit Hour System GENN004 Lab 08 Vectorized Code ___________________________________________________________________ All scripts should be stored in your FirstLastName folder and have clear names 1. Vectorize this code! Write one assignment statement that will accomplish exactly the same thing as the given code (assume that the variable vec has been initialized): 1.a) result= 0; for i= 1:length(vec) result = result + vec(i); end 1.b) result= 0; for i= 1:length(vec) if rem(i,2)==0 result = result + vec(i); end 1.c) result= 0; for i= 1:length(vec) if rem(vec(i),2)==0 result = result + vec(i); end 1.d) result= 0; for i= 1:length(vec) if rem(vec(i),2)==0 result = result + 1; end 2. Create a vector of five random integers, each in the range from –10 to 10. Perform each of the following using only vectorized code: Lab 08 1/3

Upload: bishoy-emile

Post on 07-Nov-2014

104 views

Category:

Documents


0 download

DESCRIPTION

matlkab 8

TRANSCRIPT

Page 1: Lab08_GENN004m_VectorizedCode

Cairo UniversityFaculty of EngineeringCredit Hour SystemGENN004

Lab 08Vectorized Code

___________________________________________________________________All scripts should be stored in your FirstLastName folder and have clear names

1. Vectorize this code! Write one assignment statement that will accomplish exactly the same thing as the given code (assume that the variable vec has been initialized):

1.a)result= 0;for i= 1:length(vec)

result = result + vec(i);end

1.b)result= 0;for i= 1:length(vec)

if rem(i,2)==0result = result + vec(i);

end

1.c)result= 0;for i= 1:length(vec)

if rem(vec(i),2)==0result = result + vec(i);

end

1.d)result= 0;for i= 1:length(vec)

if rem(vec(i),2)==0result = result + 1;

end

2. Create a vector of five random integers, each in the range from –10 to 10. Perform each of the fol-lowing using only vectorized code:

Subtract 3 from each element Count how many are positive Get the absolute value of each element Find the maximum.

3. Write a program to count the even and the odd elements in an array. Assuming the array = [1 7 8 2 7], the output should be even=2 and odd=3.

Lab 08 1/2

Page 2: Lab08_GENN004m_VectorizedCode

Cairo UniversityFaculty of EngineeringCredit Hour SystemGENN004

4. A company is calibrating some measuring instrumentation and has measured the radius and height of one cylinder 10 separate times; the measurements are in vector variables r and h. Use vectorized code to find the volume from each trial, which is given by pi*r^2*h. Also, use logical indexing first to make sure that all measurements were valid (> 0).

5. Write a program that reads an array of students’ grades from the user and creates an array of characters containing the corresponding alphanumeric grades. Use a function to do the conversion.

Example:Enter students grades:[98 23 77 88 99 65 57]Alphanumeric grades [A F C B A D F]

6. Write a program to calculate the distance between two sets of points.

Example:P1=[1 2; 0 0; 10 12; 3 5]P2=[4 5; 3 5;1 7; 3 4]Dist=[ 4.2426 5.8310 10.2956 1.0000]

7. Write a program that reads an array of students’ grades from the user and creates 1x5 array containing the number of A, B, C, D, and F students.

Example:Enter students grades:[98 23 77 88 99 65 57]Students statisticsA B C D F2 1 1 1 2

Lab 08 2/2