Wednesday, December 30, 2015

MatLab problems

Problems
Write Matlab code that does the following:
(1) Create a vector of the numbers 1 to 100
(2) Create a vector of the even numbers between 1 and 49 in reverse order (that
is [48, 46, . . . , 2])
(3) Assign the matrix

 | 1 2 3 |
 | 4 5 6 |
 | 3 2 1 |

 to a variable a
(4) Add the rst column of a to the second column of a
(5) Add 3 to every element of a
(6) Add 3 to every element of the second row of a
(7) Assign the vector [5; 7; 11; 13; 17] to the variable x. Assign the vector
[1; 2; 3; 5; 7] to the variable y.
(8) Find the sum of the component-wise multiplication of x and y
(9) Raise each element of x to the power of the corresponding element in y
(10) Divide each element of x by the corresponding element in y
(11) Create a vector with the elements
  • [1; 1=2; 1=3; : : : ; 1=100]
  • [0; 1=2; 2=3; 3=4; : : : ; 99=100]
Answer
----------
%1
[1:100]

%2
[48:-2:1]

%3
a = [1 2 3; 4 5 6; 3 2 1]

%4
a(:,1) + a(:,2)

%5
a + 3

%6
a(2,:) = a(2,:) + 3

%7
x = [5 7 11 13 17]
y = [1 2 3 5 7]

%8
sum(x .* y)

%9
x .^ y

%10
x ./ y

%11
vector1 = 1 ./ [1:100];
vector2 = [0:99] ./ [1:100];

rats( vector1(1:3) )
rats( vector2(1:4) )

No comments:

Post a Comment