

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!


Exercise 1. Declaring vectors and assigning values
Try the following ways to declare and initialize vectors (vectors = one dimensional arrays) in Matlab v1 = [1,2,3,4,5,6,7,8]; v2 = [1;2;3;4;5;6;7;8]; v3= [1:1:100]; v4=[1:3:100]; v5=ones(1,10); v6=ones(10,1); v7=zeros(10,1); v8=linspace(0,100,50) % 50 linearly spaced elements v9=randi([7 49], 1,8) % row vector of 8 random numbers, range 7-
Exercise 2. Declaring matrices and assigning values Declare and assign values to the following matrices
Exercise 3. Random values Write a MATLAB program to declare a vector and assign random integer values to its elements. The values must be in the range 1:100, and the length of the vector n must also be a random value in the same range. After assigning the values, the program must print the number of elements created and their values. After testing your script, extend it to display only the elements with odd indexes (index=position in the array).
First, write the code without vectorization (use randi to generate a single random number, and call it in a loop). Next, try to vectorize your code
Exercise 4. Concatenate (join vectors) Write a program that first reads the values for all elements of two different column vectors (entered by the user), and then concatenates these two vectors into a third column vector. Display the three vectors. The size of each vector is known, and must be declared as a variable. You can choose the values you like. Write both a vectorized version with the concatenation operator, and a version using loops (copy element by element into a third vector)
Exercise 5. Find maximum in matrix Write a program that reads all the values for all elements of a 3 x 4 matrix and finds the maximum value, as well as the index (row, column) of such value
Exercise 6. Add up columns in matrix Write a program that creates a matrix of 3 x 4 random values and sums the values of all the columns
Exercise 7. Main diagonal Write a MatLab program that declares a 2-dimension array of size NxN. Assign value 1 to all the elements of the array. Display the array, next assign value 0 to the elements in the main diagonal and display it again to show the changes
Exercise 8. Check if different Write a MATLAB program to define a vector of 10 random values within [10, 30]. Check if all the elements are different and display the result
Exercise 9. Green Fuel Cells Corporation (3D array) Write a program to store the number of hours worked in one week by the employees of the Green Fuel Cells Corporation. The company has two factories employing eight people each, and working from Monday to Friday. The employees are identified by a numerical code, from 1 to 8. a. Create the 3-D matrix hours , to store in each element the number of hours that an employee has worked in a day of the week. This matrix must be randomly initialized, being each value in the range 6 to 9. b. Display the hours worked by the employee 5 of the factory number two, on Monday. c. Print the total amount of hours worked by the employees of factory number one. d. Print the number of hours worked by the employee who worked the most in the factory number two. e. How many employees worked more than 37 hours (along the week) in the factory number one?
Exercise 10. Marks Write a MATLAB program to calculate the weighted mean grade of a student in his/her degree. The degree consists of 5 different courses. To pass a course the student must pass an exam, but he/she can sit the exam again up to 6 times until he/she passes. If the student doesn't pass after the sixth exam he/she is expelled from the university
The program must ask for the marks of the student for all the 5 courses, repeating the question for as much as 6 times (6 calls), until it is a pass (greater or equal to five). In case the mark for all 6 exams is lower than 5 for any of the courses, the program will print the message "student expelled" and end.
Data must be stored using two vectors, the first one (marks) stores the mark for each of the five courses and the second one (calls) stores the number of the call in which the student passed (first, second, third,…).
After reading the marks, the program must find and print the weighted average mark of the student applying the following weights: 1.25 if the course is passed in the first exam, 1 if the course is passed in the second, 0. for the third, 0.75 for the fourth, 0.6 for the fifth and 0.5 for the sixth. Store the weights in a vector of size 6, called weights (weights []={1.25,1.0,0.9,0.75,0.6,0.5};)
Example: Let’s assume the marks are: course 1: 4 – 4.5 – 4.7 – 5, second course 3-5, third course 6, fourth course 8, and fifth course 10. Values for vector marks will be [5, 5, 6, 8, 10], values for vector exam will be be [4, 2, 1, 1, 1] and the final mark will be (5weights (4) + 5weights (2) + 6weights (1) + 8weights (1) + 10*weights (1)) / 5 = 7.75 (check)