Science:Math Exam Resources/Courses/MATH152/April 2011/Question A 08
• QA 1 • QA 2 • QA 3 • QA 4 • QA 5 • QA 6 • QA 7 • QA 8 • QA 9 • QA 10 • QA 11 • QA 12 • QA 13 • QA 14 • QA 15 • QA 16 • QA 17 • QA 18 • QA 19 • QA 20 • QA 21 • QA 22 • QA 23 • QA 24 • QA 25 • QA 26 • QA 27 • QA 28 • QA 29 • QA 30 • QB 1(a) • QB 1(b) • QB 1(c) • QB 2(a) • QB 2(b) • QB 3(a) • QB 3(b) • QB 3(c) • QB 4(a) • QB 4(b) • QB 4(c) • QB 4(d) • QB 5(a) • QB 5(b) • QB 5(c) • QB 6(a) • QB 6(b) • QB 6(c) • QB 6(d) • QB 6(e) •
Question A 08 |
---|
Write the matrix that would result from the following lines of MATLAB code: A = zeros(4,4); for k=1:4 A(k,k) = -k; end for k=1:4 A(1,k) = 1; end |
Make sure you understand the problem fully: What is the question asking you to do? Are there specific conditions or constraints that you should take note of? How will you know if your answer is correct from your work only? Can you rephrase the question in your own words in a way that makes sense to you? |
If you are stuck, check the hint below. Consider it for a while. Does it give you a new idea on how to approach the problem? If so, try it! |
Hint |
---|
Recall that Matlab stores matrices first by rows then by columns. Therefore A(i,j) is the element in the ith row and the jth column. |
Checking a solution serves two purposes: helping you if, after having used the hint, you still are stuck on the problem; or if you have solved the problem and would like to check your work.
|
Solution |
---|
Found a typo? Is this solution unclear? Let us know here.
Please rate my easiness! It's quick and helps everyone guide their studies. First we see the line A=zeros(4,4); from the hint we notice that Matlab stores by row then by column so this is an instruction to create a matrix filled with zeros that has 4 rows and 4 columns. Therefore after this call we have The next lines for k=1:4 A(k,k)=-k; end initiates a for loop or a counting loop over a variable k. It says to start at k=1, perform whatever is inside the loop, increase k by 1 and then repeat until k=4. Here we see that the kth row and the kth column of A from 1 ≤ k ≤ 4 is filled with -k. Therefore, after this for loop we have The next lines are for k=1:4 A(1,k)=1; end which is another for loop. This time we always stay on row 1 but column k gets filled with 1 for 1 ≤ k ≤ 4. Notice since we still start at k=1 then the first entry we write to is A(1,1) which already has a -1 in it. Therefore, this will get overwritten with a 1. The matrix becomes Since there are no more lines of code, this is the final matrix. |