Chapter_2A_Extra_Problems

.docx

School

Boston University *

*We aren’t endorsed by this school

Course

125

Subject

Mathematics

Date

Jan 9, 2024

Type

docx

Pages

5

Uploaded by victoria.agc

Report
ENG EK 125 Chapter 2A Extra Problems Name: Victoria Gonzalez Canalle Section: _________ 1) The following expression creates a row vector. >> colvec = 1:3' colvec = 1 2 3 >> Why is that? Because the ‘ transposes the column vector into a row vector 2) Let’s say that we have created a vector variable vec . Here is an example. >> vec = [3:5 11 0 -2 33] vec = 3 4 5 11 0 -2 33 All of the following expressions refer to the last element in the vector. Which expressions are “good”, meaning general? >> vec(end) ans = 33 GOOD >> vec(7) ans = 33 BAD >> vec(length(vec)) ans = 33 GOOD >> vec(numel(vec)) ans = 33 GOOD
3) Let’s say that we have created a matrix variable mat . Here is an example. >> mat = randi([-5 10], 2,4) mat = 5 -3 2 0 5 -4 10 9 All of the following expressions refer to the last element in the matrix. Which expressions are “good”? For a matrix, “good” means general and using subscripted indexing, not linear indexing. >> mat(end,end) ans = 9 GOOD >> [r, c] = size(mat); >> mat(r,c) ans = 9 GOOD >> mat(end) ans = 9 BAD >> mat(2,4) ans = 9 BAD >> mat(8) ans = 9 BAD The following expression might seem like it would work, but it does not. Why? >> mat(size(mat)) ans = 5 -4 Because it depends on the linear indexing and not the subscripted indexing. So it is giving the elements in positions r and c.
4) Assume that you have a vector variable vec. Write an expression that refers to only the elements with odd-numbered subscripts in vec , regardless of the length of the vector. Vec(1:2:end) 5) Given the matrix: >> mat = randi([1 20], 3,5) mat = 6 17 7 13 17 17 5 4 10 12 6 19 6 8 11 Why wouldn’t this work: mat(2:3, 1:3) = ones(2) Because the size of the matrices do not agree. Left side is 2 x 3 and the right one is 2 x 2. 6) What is the difference between fliplr(mat) and mat = fliplr(mat)? One assigns the value to mat and the other just flips mat, without assigning it back, so the original ma stays. 7) Given the matrix variable mat : mat = 1 3 5 2 6 3 What would be the result of the following expressions: cumsum(mat) ans = 1 3 5 3 9 8 sum(sum(mat)) ans = 20 sign(mat) ans = 1 1 1 1 1 1
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help