0% found this document useful (0 votes)
6 views4 pages

Bme HW 2

1. Three different methods are provided to create a row vector with values from 6.9 to 3.3 decreasing by 1.2 each time. 2. A column vector is created with values from -30 to 40 increasing by 0.5. 3. A 7x6 matrix of zeros is created and then modified to replace the third row with 3s and the second column with a new vector.

Uploaded by

ashtonlang12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Bme HW 2

1. Three different methods are provided to create a row vector with values from 6.9 to 3.3 decreasing by 1.2 each time. 2. A column vector is created with values from -30 to 40 increasing by 0.5. 3. A 7x6 matrix of zeros is created and then modified to replace the third row with 3s and the second column with a new vector.

Uploaded by

ashtonlang12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Three different methods to create the following row vector


a. vec1= [6.9000, 5.7000, 4.5000, 3.3000];
b. vec2 = linspace(6.9, 3.3, 4);
c. vec3 = [];
>> for i = 1:4
vec3 = [vec3, 6.9 - (i-1)*1.2];
end

2. Type a Matlab expression to create a column vector that has values from -30 to
40 in step of 0.5.
a. col_vec = (-30:0.5:40)';
3. Write Matlab expressions to generate a 7 (row) x 6 (col) matrix variable mat with
all zeros. Then (1) replace the third row with all “3”s, (2) replace the 2nd column
with a new vector: [1, 4, 7, 10, 13, 16, 19].
a. I’m not totally sure what you are asking, I’m sorry. This is what I took it as

>> mat = zeros(7, 6);

>> mat(3, :) = 3; Replace the third row with all "3"s

>> mat(:, 2) = [1, 4, 7, 10, 13, 16, 19]; Replace the second column
4. Write Matlab expressions to create a 5 x 4 matrix of integer random numbers in
the inclusive range from 50 to 80. Then write three Matlab expressions to:
a. rand_mat = randi([50, 80], 5, 4);
● Find the maximum value in each column.
○ max_col = max(rand_mat);
● Find the maximum value in each row.
○ max_row = max(rand_mat, [], 2);
● Find the maximum value in the entire matrix.
○ max_all = max(max(rand_mat));

5. Write Matlab expressions to extract only the elements at odd positions in a vector
(i.e. the 1st, 3rd, 5th, 7th..., elements of the vector), regardless of the length of
the vector. Make sure your expression can work on vectors with odd or even
number of elements.
a. myVector = [1,2,3,4,5,6,7,8,9,10];
b. OddElements = myVector(1:2*end );
6. A vector OrderData stores a sequence of numbers that represent the unit price
and order quantity of several products. The variable can be defined using the
following expression in Matlab command window:
>>OrderData=[25.3,18,14.0,82,43.0,17,22.5,33,21.5,35,9.5,112,65.2,35]; As an
example, the first product is priced at $25.30 per unit and 18 of it are ordered; the
second item is priced at $14.0 per unit and 82 are ordered, and so on.

1. OrderData =
[25.3,18,14.0,82,43,0,17,22.5,33,21.5,35,21.5,35,9.5,112,65.2,35];

2. Unitprices = OrderData(1:2:end);

3. orderQuantities = OrderData(2:2:end);

4. TotalPrices = Unitprices.* orderQuantities

7. 7) Use Matlab codes to create two different vectors to separately store the
numerators and denominators of the following sequence of 50 fractional
numbers: 1/3, 2/5, 3/7, 4/9, 5/11, 6/13, ......, 50/101
As seen in the numbers above, the numerators go from 1 to 50 with an increment
of 1; whereas the denominators go from 3 to 101 with an increment of 2.Please
use the dot division of two vectors to calculate the summation of this sequence:
a. numerators = 1:50;
b. denominators = 3:2:101;
c. sequence = numerators ./ denominators
d. sum_sequence = sum(sequence);
e. I got 24.0262
8. If MAT is a 3x2 random matrix, please explain what will be produced by MAT>0.
Use an example to demonstrate your explanation.

MAT>0 is a logical comparison in MATLAB that makes every element of the matrix
greater than 0. It then makes a matrix of the same size as MAT where every part is
either 1, which is true, saying every element is greater than 0, or 0, which is false, if
every number is not greater than 0.

EX:

a. MAT = randn(3, 2);


b. result = MAT > 0;
c. disp("Original Matrix:"); Original Matrix:
d. disp(MAT);

-0.1924 -1.4023

0.8886 -1.4224

-0.7648 0.4882

e. disp("Result of MAT>0:"); Result of MAT>0:


f. disp(result);

0 0

1 0

0 1

You might also like