HW 1: MATLAB
Read and execute the commands described Sections 1–10 of Basic Concepts in Matlab
(including creating the function mydist.m) and then answer the following questions (you can
diary command in Matlab (see help diary)
to record your commands and results to a text file):
1. Given the array x = [3 1 5 7 9 2 6], provide the single command (one line of
code with at most one assignment (i.e., equals sign =)) needed to perform the following
actions. Unless noted, all of the actions should not modify x; instead, the result of the
action will be assigned to the default variable ans.
(a) Extract the forth element from x
(b) Extract all but the last element from x
(c) Extract the first, third, first, fifth, and first element from x
(d) Reverse the elements of x
(e) Calculate the sum of all of the elements of x
(f) Calculate the sum of the first to the ith element of x, for all elements of x
(g) Set the second and sixth elements of x equal to zero
(h) Modify x by deleting its third element
(i) Using the result from (h), modify x by converting it into a 2 3 matrix, where the
first row of the matrix has the first three elements of x 2
2. Let x = [6 2 1 4] and A = [2 7 9 7; 3 1 4 6; 8 1 2 5], provide the
single command needed to:
(a) Add x to each row of A
(b) Add x to the sum of each column of A
(c) Add the sum of x to each element of A
(d) Calculate the sum of the element-by-element product of each row of A and x
3. Given that x = [1 5 2 7 9 0 1] and y = [5 2 2 6 0 0 2], provide the
single command needed to:
(a) Extract from x those values that are greater than the corresponding values of y
(b) Extract from x those values that are both greater than the corresponding values of y
and less than 6
(c) Extract from x those values that are either less than 2 or greater than 6
(d) Modify y by adding 1 to each of its nonzero values
(e) Divide each element of y by the corresponding element of x as long as the element of
x is nonzero (to avoid dividing by zero)
(f) Modify y by setting all of its zero values to 1
4. Modify the Minimum-Distance Location example in Basic Concepts so that it can be used
to find the location that minimizes the maximum distance traveled between x and the three
points in P.
5. Modify the Minimum-Distance Location example in Basic Concepts so that it can be used
to find the location that minimizes the sum of distance traveled assuming that 3, 2, and 4
trips, respectively, are made between x and the three points in P.
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。