Science:Math Exam Resources/Courses/MATH152/April 2011/Question A 06
• 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 06 

What is the output after the following lines of MATLAB code? A = [1 2 3; 4 5 6; 7 8 9]; A(2,:) 
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 

What is the difference between a space and a semicolon when defining a matrix. What does the colon do? 
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. For the first line of code A=[1 2 3;4 5 6;7 8 9]; we recall that when declaring a matrix in Matlab, square brackets [ and ] start and end the declaration. While inside a space indicates to move to the next column while a semicolon instructs to start a new row. Therefore we place the entries 1, 2, and 3 in the three columns of the first row; 4, 5, and 6 in the three columns of the second row and 7, 8, and 9 in the three columns of the last row. Therefore the matrix generated is The semicolon at the end of the line indicates that output is suppressed and so the matrix will not be displayed. For the second line of code A(2,:) we recall that Matlab recognizes entries in a matrix first by row and column so a call A(i,j) would return an element in row i and column j. The colon instructs Matlab to take all the entries of a given field. Therefore A(:,j) instructs Matlab to output all entries in column j while A(i,:) instructs Matlab to output all of the entries in row i. Therefore A(2,:) instructs Matlab to output all of the entries from row 2. If we look at out matrix then the output would be [4 5 6] Notice the lack of semicolon on this output indicates that the output is not suppressed. 